mardi 29 mai 2018

Laravel variable model polymorphic

I have a table as follows in my database table area_blocks;

id owner_type owner_id created_at updated_at

in the owner_type fields it has the Eloquent Model name and the owner_id is the id of that model in the database. example db;

db: area_blocks

id | owner_type            | owner_id 
1  | App\Models\Title      | 3 
2  | App\Models\Title      | 4
3  | App\Models\Textarea   | 1

So I'm expecting when I fetch all of these to also eager load the relevant field from the eloquent model stored in owner_type.

Is there an eloquent relationship that can bring back that record from the owner_type field using eager loading? I've tried $this->morphTo() e.g.

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

but that is returned as null. Any ideas how this can be done?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IUIz3l
via IFTTT

Aucun commentaire:

Enregistrer un commentaire