Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Atef95's avatar

Custom blade directive for date format

Hey guys

I'm trying to customize created_at field in laravel and I created a custom blade directive for that in order to apply it in all occurences..

AppServiceProivder.php

 Blade::directive('formatDate', function ($date) {
            
            return "<?php echo $date->format('d-m-Y') à $date->timezone('Europe/Paris')->format('H:i:s')  ?>";
        });

My view

<p>@formatDate($product->created_at) </p>

this throws the following error :

Trying to get property 'format' of non-object

0 likes
1 reply
BezhanSalleh's avatar

@atef95 use the following and be sure to run php artisan optimize:clear after you edit the directive for the changes to take effect.

Blade::directive('formatDate', function ($date) {
    return "<?php echo ($date)->format('d-m-Y').'  '.($date)->timezone('Europe/Paris')->format('H:i:s'); ?>";
                });
~~

Please or to participate in this conversation.