laravel
Junior
Explain Blade directives
Short Answer
Blade directives are shorthand syntax for PHP control structures in templates, starting with @ symbol: @if, @foreach, @extends, @yield, @csrf.
Detailed Answer
Blade provides directives that compile to clean PHP:
Conditional directives:
- @if / @elseif / @else / @endif
- @unless (opposite of @if)
- @isset / @empty
Loop directives:
- @foreach / @endforeach
- @for / @endfor
- @while / @endwhile
- @forelse (empty collection handling)
Layout directives:
- @extends (inherit layout)
- @section / @endsection (define content)
- @yield (output section)
- @include (embed view)
Other:
- @csrf (CSRF token)
- @method (HTTP method spoofing)
- @json (output JSON)
- @vite (Vite asset loading)
- @error (validation errors)
- @can / @endcan (authorization)
Code Example
Example
@extends('layouts.app')
@section('content')
@if($posts->isNotEmpty())
@foreach($posts as $post)
<article>
<h2>{{ $post->title }}</h2>
@if($post->is_featured)
<span class="badge">Featured</span>
@endif
@unless(auth()->guest())
@can('update', $post)
<a href="{{ route('posts.edit', $post) }}">Edit</a>
@endcan
@endunless
</article>
@endforeach
@else
<p>No posts found.</p>
@endif
{{ $posts->links() }}
@endsection
@error('title')
<span class="error">{{ $message }}</span>
@enderror
Interview Tip
Mention that Blade compiles to plain PHP for performance. Show awareness of @once, @production, @env directives for environment-specific content.