Blade is a powerful templating engine provided with Laravel for working with PHP and HTML. It provides various directives to make your views more expressive and concise.
- Laravel Blade Cheat Sheet
The @extends
directive is used to inherit a layout.
@extends('layout.name')
The @section
directive defines a section that can be overwritten in child views.
@section('sectionName')
// content of the section
@endsection
The @yield
directive is used to output the contents of a section.
@yield('sectionName')
The @parent
directive is used to display the content of the parent section.
@section('sectionName')
@parent
// additional content
@endsection
The @include
directive is used to include a sub-view.
@include('view.name')
The @if
directive is used for conditional statements.
@if($condition)
// code to execute if condition is true
@elseif($anotherCondition)
// code to execute if another condition is true
@else
// code to execute if none of the conditions are true
@endif
The @empty
directive checks if a variable is empty.
@empty($variable)
// code to execute if the variable is empty
@endempty
The @foreach
directive is used to iterate over arrays or objects.
@foreach($items as $item)
// code to execute for each item
@endforeach
The @for
directive is used for simple loop iterations.
@for($i = 0; $i < 5; $i++)
// code to execute for each iteration
@endfor
The @forelse
directive is used to display a list of items with an optional message if the list is empty.
@forelse($items as $item)
// code to execute for each item
@empty
// code to execute if the list is empty
@endforelse
The @while
directive is used for indefinite loop iterations.
@while($condition)
// code to execute while the condition is true
@endwhile
The {{ }}
directive is used to echo the contents of a variable.
{{ $variable }}
The {!! !!}
directive outputs the contents without escaping HTML entities.
{!! $htmlContent !!}
The {{-- --}}
directive is used to add comments in Blade templates.
{{-- This is a blade comment --}}
The @auth
directive checks if the user is authenticated.
@auth
// code to execute if the user is authenticated
@endauth
The @guest
directive checks if the user is a guest.
@guest
// code to execute if the user is a guest
@endguest
The @can
directive checks if the user has a certain permission.
@can('permissionName')
// code to execute if the user has the permission
@endcan
The @cannot
directive checks if the user does not have a certain permission.
@cannot('permissionName')
// code to execute if the user does not have the permission
@endcannot
The @canany
directive checks if the user has any of the given permissions.
@canany(['permission1', 'permission2', ... , 'permissionN'])
// code to execute if the user has any of the given permissions
@endcanany
The @env
directive checks the application environment.
@env('local')
// code to execute if the application is in the local environment
@endenv
The @production
directive checks if the application is in production environment.
@production
// code to execute if the application is in production environment
@endproduction
These are the commonly used Blade directives in Laravel for templating and controlling the presentation logic of your views.