This example is for Laravel 5.5 and should be compatible with 5.6 onwards but no guarantees!
The problem
When using date and datetime fields in Laravel it is really useful to add them to the $dates array in the Eloquent model so that they are automatically cast to Carbon instances.
Then in the views, the format can be easily specified;
<td>{{ $task->completed_at->format('d-m-Y H:i') }}</td>
This works fine until the date is null. In our example, the task completed date is not set until the task is completed, and with the above code, sometimes we are trying to apply Carbon format to a null object.
The view then starts to get messy, checking if the date is null before outputting the Carbon format.
Enter Blade. Using Blade we can hide some of this complexity from our views.
If you haven’t written Blade directives before, they can be confusing. The purpose of a blade directive is to return a string of PHP that can be inserted into the cached view file, and then executed later, when the view is rendered. This means it is not possible to directly interact with any of your data within the blade component itself.
Creating a blade directive
If you are creating just a single blade Directive then its possible to just put this in the AppServiceProvider, but as I might create more, and also be able to easily lift this and put it in other projects, I decided to create a BladeServiceProvider.php file and then put all my directives in there.
Start with the template for a Service Provider php artisan make:provider BladeServiceProvider
and add this new provider to your config/app.php file
/* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\BladeServiceProvider::class,
Add our new directive to the BladeServiceProvider boot method
public function boot() { /** * @date blade directive * use as @date($object->datefield) * or with a format @date($object->datefield,'m/d/Y') */ Blade::directive('date', function ($expression) { $default = "'d-m-Y H:i'"; //set default format if not present in $expression $parts = str_getcsv($expression); $parts[1] = (isset($parts[1]))?$parts[1]:$default; return '<?php if(' . $parts[0] . '){ echo e(' . $parts[0] . '->format(' . $parts[1] . ')); } ?>'; }); }
Line 8 is the start of our Blade definition. Make sure you import Illuminate\Support\Facades\Blade
at the top of the service provider. Here we declare ‘date’ is the name of our new directive. When the Blade extension is used it will be passed $expression, and this will be a single string from which we have to parse our parameters.
Line 12 breaks the expression into multiple parts as an array
Line 14 ensures that we have a $parts[1]
element so that we can always pass a string to the format method. The default value is specified on line 10 and will be used as a fallback if the format is not specified in our view.
Finally, Line 16 is the meat of this Blade. This strange concatenation of strings and parts is building up the final output that will go into our cached view file for processing when the final view is rendered. The function of this is to create an in-line php code that checks if the value is not null and if so, echo it with the Carbon format command appended. If the value is null then nothing is echoed.
What have we created?
We now have a Blade directive that can format dates if they are not null and optionally accepts a date format
<td>@date($task->completed_at)</td> <td>@date($task->archived_at,'Y-m-d')</td>
This last example, produces Blade output of
<td><?php if($task->archived_at){ echo e($task->archived_at->format('Y-m-d')); } ?></td>
Tips!
Working with Blade can be extremely time consuming. Don’t underestimate the amount of time it takes fiddling with the output string.
Don’t forget, you cannot interact with the data within the blade directive itself.
Always clear cached views php artisan view:clear
each time you make a change to your BladeServiceProvider