View Composers and Custom Blade Directives are some of the advanced topics of Laravel. In this tutorial, I will discuss with you, some laravel advanced topics . Those are custom blade directives and laravel view, composers.
Laravel View Composers and Custom Blade Directives with Examples

Well, sometimes you may need to share data with all views that are rendered by your application. In order to do this, you may do so using the View
facade’s share
method. Typically, you should place calls to the share
the method within a service provider’s boot
method.
App\Providers\AppServiceProvider
public function boot()
{
View::share('globalValue', 'I am global view variable!');
View::share('globalCity', City::get());
}
Well, now you can get access all view file City model’s data by $globalCity variables. But there is a problem. The problem is whenever a view renders, it calls a query in the cities table. Sometimes few views file we don’t need to the cities data but will be rendered by default. Which is an application query performing issue. To solve this issue, we can use laravel view composers.
Laravel View Composers
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location. View composers may prove particularly useful if the same view is returned by multiple routes or controllers within your application and always needs a particular piece of data.
Well, let’s see an example of how you use view composers. To do this you can create a dedicated provider class by this command.
php artisan make:provider ViewServiceProvider
Well, now to go the file App\Providers\ViewServiceProvider
. Inside the boot functions, your code will be like this.
By the way, if you create a new service provider to contain your view composer registrations, you will need to add the service provider to the providers
array in the config/app.php
configuration file.
public function boot()
{
View::composer(['welcome', 'view.*'], function ($view) {
$view->with('globalCity', City::get());
});
}
Well, this View facade is Illuminate\Support\Facades\View. Now you can access globalCity only welcome page and all view folder files. Sometimes your logic will more complex, so it’s good to create another class file for your compose logic.
Well, Laravel by default doen’t provide you for create a composer. You have to create it manually. So create a folder inside app’s Http folder. The folder name should be View. Well, then inside the View folder create another folder which name should be Composers. Well, inside the composers folder you have to create all composers class file.
Now create a class which name is CityComposer. Make a method which name should be compose.
app/Http/View/Composers/CityComposer.php
<?php
namespace App\Http\View\Composers;
use App\Models\City;
use Illuminate\View\View;
class CityComposer
{
public function compose(View $view)
{
$view->with('globalCity', City::get());
}
}
Read More : Task Scheduling Laravel example with Crontab
Now you have to register in service provider like this.
View::composer(['welcome', 'view.*'], CityComposer::class);
Now you can use the cities in your defined folder.
Custom Blade directives
Blade allows you to define your own custom directives using the directive
method. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.
app/Providers/AppServiceProvider.php
Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('m/d/Y'); ?>";
});
Blade::directive('ars', function ($expression) {
return "<?php echo strtoupper($expression)?>";
});
Blade::directive('route', function ($expression) {
//dd($expression);
return "<?php echo route($expression); ?>";
});
Well, I have wrote couple of blade directive examples, you can use in blade file like this.
<a href="@route('user',['user' => 5])">al</a>
@ars('and')
@datetime(now())
<br>
@ars('shahin')
Well, Hope this tutorial will help you.