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

matthallett's avatar

HasMany on a date field

I've tried to create a hasMany relationship based on a date field.

public function payments()
    {
        return $this->hasMany('App\Payment', 'date', 'payment_date');
    }

On it's own, it works. The problem comes when I add the date columns to their respective models' $dates attribute.

If the parent has the $dates attribute:

ErrorException in HasOneOrMany.php line 119: Illegal offset type in isset or empty

If the child has the $dates attribute, or both have it:

ErrorException in HasOneOrMany.php line 160: Illegal offset type

Done a bit of Googling and seen a couple of examples of other people running into this, but not sure of the solution. Well obviously I can just leave out the $dates, but I think it's better practice to use it (otherwise I'll just have to remember to the dates in Carbon::parse before formatting).

0 likes
1 reply
tekmi's avatar

@matthallett Maybe you could try to define payment date accessor like

public function getPaymentDateAttribute()

instead of adding payment_date to $dates array? You could then return Carbon instance from there.

Please or to participate in this conversation.