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
@endsectionThe @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
@endsectionThe @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
@endifThe @empty directive checks if a variable is empty.
@empty($variable)
// code to execute if the variable is empty
@endemptyThe @foreach directive is used to iterate over arrays or objects.
@foreach($items as $item)
// code to execute for each item
@endforeachThe @for directive is used for simple loop iterations.
@for($i = 0; $i < 5; $i++)
// code to execute for each iteration
@endforThe @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
@endforelseThe @while directive is used for indefinite loop iterations.
@while($condition)
// code to execute while the condition is true
@endwhileThe {{ }} 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
@endauthThe @guest directive checks if the user is a guest.
@guest
// code to execute if the user is a guest
@endguestThe @can directive checks if the user has a certain permission.
@can('permissionName')
// code to execute if the user has the permission
@endcanThe @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
@endcannotThe @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
@endcananyThe @env directive checks the application environment.
@env('local')
// code to execute if the application is in the local environment
@endenvThe @production directive checks if the application is in production environment.
@production
// code to execute if the application is in production environment
@endproductionThese are the commonly used Blade directives in Laravel for templating and controlling the presentation logic of your views.