Laravel
Beginner
1 min read
Blade Directives and Service Injection
Example
{{-- Authorization directives --}}
@can('update', $post)
<a href="{{ route('posts.edit', $post) }}">Edit</a>
@endcan
@canany(['update', 'delete'], $post)
<div class="action-buttons">...</div>
@endcanany
{{-- Authentication directives --}}
@auth
<span>Hello, {{ auth()->user()->name }}</span>
@else
<a href="{{ route('login') }}">Log in</a>
@endauth
{{-- CSRF and method spoofing --}}
<form action="{{ route('posts.update', $post) }}" method="POST">
@csrf
@method('PUT')
<input type="text" name="title" value="{{ old('title', $post->title) }}">
<button type="submit">Update</button>
</form>
{{-- Service injection --}}
@inject('metrics', 'App\Services\MetricsService')
<p>Total posts: {{ $metrics->totalPosts() }}</p>
{{-- Registering a custom directive in AppServiceProvider::boot() --}}
{{-- \Blade::directive('money', fn($e) => "<?php echo number_format($e, 2); ?>"); --}}
{{-- Usage: @money($post->price) --}}
Related Resources
Laravel Reference
Complete tag & property list
Laravel How-To Guides
Step-by-step practical guides
Laravel Exercises
Practice what you've learned
More in Laravel