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

hellotoby's avatar

Date diff accessor always returns null

I've created a date diff accessor in my User model to determine the number of days a user has left in their Cashier free trial. The method looks like this:

Model - returns null

public function getRemainingTrialDaysAttribute() {
        return now()->diffInDays($this->trial_ends_at, false);
}

When I try and access this method in my controller it always returns null, however the same code when written directly in the controller will return the correct date.

Controller - returns integer (correct)

$this->trialDaysRemaining = now()->diffInDays($this->user->trial_ends_at, false);

Can anyone explain to me why it returns null when called as an accessor from the User model?

0 likes
3 replies
hellotoby's avatar

Yes I have the date mutator set in the model.

dumping both now() and $this->trial_ends_at in the User model return a Carbon instance.

Snapey's avatar

How if you use the attributes ?

return now()->diffInDays($this->attributes['trial_ends_at'], false);

Please or to participate in this conversation.