jeudi 22 mars 2018

Laravel: How to make ralationship accessor

I have relations in model UserTasks to Review model

public function reviews()
{
    return $this->hasMany(Review::class,'task_id');
}

I'm loading rewiews using with

UserTasks::with('reviews');

And I want to format reviews relation output using accessors in UserTasks model

public function getReviewsAttribute($rewiews)
{
    foreach ($rewiews as &$rewiew) {
        // DO SOMETHING WITH REVIEWS
    }
    return $rewiews; 
}

Unfortunately, this does not work

Relationships loaded using with do not pay attention to accessor

I read that it is almost impossible, but maybe someone know the way to format loaded relations using accessors



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire