USEFUL LARAVEL BLADE DIRECTIVES

|
| By Webner

Laravel Blade Directives

Laravel Blade Directives are the functions of Laravel’s templating engine that are basically a piece of code that covers the complex yet common and repetitive code written in PHP. Many of these directives are used while working on blade files but some of the unused but useful blade directives are mentioned below, which will help you in making code repetition free and more like an MVC’s view file.

1. @php

WITHOUT DIRECTIVE

<?php
echo(“Without Directive”);
?>

WITH DIRECTIVE

@php
echo(“Without Directive”);
@endphp

2. @each

WITHOUT @each

@foreach($posts as $post)
@include(‘pages.november_posts’,[‘post’ => $post])
@endforeach

WITH @each

@each(‘pages.november_posts’,$post, ‘post’)

3. @forelse

WITHOUT @forelse

@if($post->count() > 0)
@foreach($posts as $post)
<li>{{$post->title }}</li>
@endforeach
@else
<li>No Post Found.</li>
@endif

WITH @forelse

@forelse($posts as $post)
<li>{{$post->title}}</li>
@empty
<li>No Post Found.</li>
@endforelse

4. @isset

WITHOUT @isset

@if(isset($posts))
<p>{{ $posts->count() }}</p>
@endif

WITH @isset

@isset($posts)
<p>{{ $posts->count() }}</p>
@endisset

5. @empty

WITHOUT @empty

@if(isset($posts))
<p>{{ $posts->count() }}</p>
@endif

WITH @empty

@empty($posts)
<p>{{ $posts->count() }}</p>
@endempty

Leave a Reply

Your email address will not be published. Required fields are marked *