mercredi 29 août 2018

Does Laravel cache polymorphic calls?

Got a polymorphic relationship like this: User -> polymorph -> subscription from various platforms. Toy but working example:

class Polymorph
{
    ...
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function subscription()
    {
        return $this->morphTo();
    }

    public function isExpired()
    {
        return $this->subscription->isExpired(); // Checks an attribute
    }

    public function isActive()
    {
        return $this->subscription->isActive(); // Checks an attribute
    }
    ...
}

class User{
    ...
    public function poly()
    {
        return $this->hasOne(Polymorph::class);
    }
    ...
}

And I'm doing:

$poly = $user->poly
$poly->isExpired(); // One DB call
$poly->isActive(); // No DB call
// etc..

It seems like Laravel caches the $this->subscription call. I'm looking at a query log as I'm calling these methods, and there is only one SELECT for the appropriate subscription object.

I looked through the docs, but don't think I found anything about it. Is it being cached? If so, what is it called or is there documentation describing it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire