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

v0sn's avatar
Level 1

Custom Blade Directive for checking date diff

Hi guys,

i am trying to create my very first own blade directive, i want this blade directive to check the difference between a given date and todays date.

Heres what i tried:

        Blade::directive('deadline', function ($date) {
            $current_date = new \DateTime('now');

            if ($date->diffInDays($current_date) >= 7 ) {
                return "class='deadline'";
            }

        });

This is giving me the error:

Call to a member function diffInDays() on string

What am i doing wrong?

0 likes
11 replies
Cronix's avatar

I'm assuming $date is just a string like the error says, something like "2018-18-23". It needs to be a carbon instance to be able to use the diffInDays() carbon method.

Try adding

if ( ! $date instanceof Carbon\Carbon) {
    $date = new \Carbon\Carbon($date);
}

to the top of the method to convert it to carbon if it's not already. I'd also change to this

$current_date = now(); // helper for Carbon::now()
v0sn's avatar
Level 1

Hello @Cronix,

sorry for the late reply..

I think the problem is something else, if i do a dd(); on $date in my custom blade directive it returns me the following: "$complaint->created_at" instead of the actual date.. why does this happen? am i missing something here?

tykus's avatar

How are you using your directive?

v0sn's avatar
Level 1

@tykus using it in the boot method of AppServiceProvider.php

tykus's avatar

I mean in your Blade template

Snapey's avatar

You cannot write blade directives like this.

They need to return a string containing the php you want to execute when the view is finally rendered

At present you are writing code that will be rendered at the time when the view is compiled, which could be days or months before

sorry, got to go to work now or I would give you an example

1 like
v0sn's avatar
Level 1

@Snapey ohhhhh, that was what i was doing wrong, i totally know what you mean! I'll try to get it to work now :- )

v0sn's avatar
Level 1
Blade::directive('test', function($arguments) {
    list($current_date, $date) = explode(',',str_replace(['(',')',' ', "'"], '', $arguments));

    return "<?php echo($date->diffInDays($current_date));
});

This should simply return the difference between the 2 given dates

@set($current_date, Carbon::now())

This is how i set $current_date using another Blade directive

@test($current_date, $complaint->created_at)

This is how i call the test directive

Im getting the following error

Trying to get property 'diffInDays' of non-object

What i dont understand is: if if put the following code in my view, it gives me the difference, and its simply the same as the blade directive above

@php
    echo($complaint->created_at->diffInDays($current_date))
@endphp

I even put the dates in the protected_dates field in my Complaint Model.. but i dont think thats the problem here.

thanks in advance!

v0sn's avatar
Level 1
Blade::directive('test', function($arguments) {
    list($current_date, $date) = explode(',',str_replace(['(',')',' ', "'"], '', $arguments));

    return "<?php echo({$date}->diffInDays({$current_date})); ?>";
});

Putting curly braces around my variables fixxed the problem, i saw this in the custom blade directive for @set which i just copied from someone else and tried it out..

I don't actually know why this fixxed my problem, could someone explain me this?

1 like
Snapey's avatar

oh, another pointer. When you make a change to the Blade component you also need to either re-save the view that uses the Blade directive, or clear the cached view.

Your blade directives are away in another file so the framework does not notice that you changed it and carries on serving the view already compiled.

(blade directives are a PITA to work with!)

1 like

Please or to participate in this conversation.