Laravel Blade Templating System Cheatsheet - everything you need to know

Laravel Blade Templating System Cheatsheet

How to check if a user is logged in, inside a blade file?

You can use the @auth directive.

  1. @auth
  2.              Hello, logged in user!
  3. @endauth

Related: @guest to check if user is a guest (not logged in)

  1. @guest
  2.             Hello guest, do you want to register?
  3. @endguest
View More Details (and 8 discussions about this topic) Here...

What is the loop variable in a @foreach loop in Blade?

If you have the following code in a Blade file:

  1. @foreach(\User::all() as $user)
  2.  
  3. {{-- ... some code here ... --}}
  4.  
  5. @endforeach

You have access to $user (obviously) but also the $loop object.

The $loop object has the following data:

  1. object(stdClass)#290 (8) {
  2.   ["iteration"]=> int(1) // starts at 1 (on first loop)
  3.    ["index"]=> int(0) // starts at 0 (on first loop)
  4.    ["remaining"]=> int(4)
  5.    ["count"]=> int(5)
  6.    ["first"]=> bool(true)
  7.    ["last"]=> bool(false)
  8.    ["depth"]=>int(1)
  9.    ["parent"]=>NULL
  10. }

So if you wanted to do something different for the first or last rows then you could easily do this:

  1. @foreach(\App\User::all() as $user)
  2.  
  3.     @if($loop->first)
  4.         Here are our users:
  5.         <div>
  6.     @endif
  7.  
  8.     {{$user->name}}
  9.  
  10.     @if($loop->last)
  11.         </div>
  12.         That was everyone!
  13.     @endif
  14.  
  15. @endforeach

PropertyDescription
$loop->indexThe index of the current loop iteration (starts at 0).
$loop->iterationThe current loop iteration (starts at 1).
$loop->remainingThe iterations remaining in the loop.
$loop->countThe total number of items in the array being iterated.
$loop->firstWhether this is the first iteration through the loop.
$loop->lastWhether this is the last iteration through the loop.
$loop->depthThe nesting level of the current loop.
$loop->parentWhen in a nested loop, the parent's loop variable.

see more

View More Details (and 10 discussions about this topic) Here...

How to get a route to directly show a view?

If you have a simple controller method that is just:

  1. public function about_me() {
  2.                 return view('static.about_me');
  3.                 }

Then you can do the following in your routes file:

  1. Route::view('/about','static.about_me');

This makes it a bit quicker to code. Sometimes it is common to end up with a bunch of functions (in a controller named something like StaticPagesController) that don't do anything apart from return a view. Using the Route::view method in your routes file means you can skip making that function.

View More Details (and 12 discussions about this topic) Here...

How to check if a (blade) view file exists

Checking if a blade view file exists is very easy.

  1.         if (\View::exists('some.view')) { ... }

Or if in a blade view:

  1. @if(\View::exists('some.view'))
  2.   ...
  3. @endif

Some related things to be aware of:

@includeIf - include a view from within blade, if the view exists

  1. @includeIf('view.name', ['some' => 'data'])

@includeFirst - Include the first view that exists (the final one would be the default shown if no others exist). In this example it would try and see if custom.admin existed (/resources/views/custom/admin.blade.php), but if it didn't then it would try admin.

  1. @includeFirst(['custom.admin', 'admin'], ['some' => 'data'])

@includeWhen - Used to include a view when a boolean condition is true

  1. @includeWhen($boolean, 'view.name', ['some' => 'data'])
View More Details (and 7 discussions about this topic) Here...

How to do comments in Blade files?

Comments in Blade are very simple!

You can either do normal PHP comments:

  1. <? /* some comment here */
  2. // or single line comments
  3. # or these :)
  4. ?>

Or blade comments:

  1. {{-- Blade comments that wil not appear in the rendered HTML output --}}

If you use an IDE that supports blade formatting (such as PHPStorm) then you can get it to output blade comments in the same style as normal comments.

View More Details (and 9 discussions about this topic) Here...