A
A
agreIT2019-11-21 13:06:57
Laravel
agreIT, 2019-11-21 13:06:57

How to load all relations in Laravel 5.1 polymorphic relation?

There is a main Pattern model that has many Layer models .
There is a Layer model that has a polymorphic relation to the Image and Text models . The Text
model has no child relationship, while the Image has a one-to-one relationship with the File model . In what way, when unloading the Pattern model , pull up all the Layers , each with its own additional relations?

Pattern::whereId($id)->with('layers.layereable')->get();

5dd65efd405de322707951.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kylin, 2019-11-22
@palkan_karabov

use Illuminate\Database\Eloquent\Relations\MorphTo;

$activities = ActivityFeed::query()
    ->with(['parentable' => function (MorphTo $morphTo) {
        $morphTo->morphWith([
            Event::class => ['calendar'],
            Photo::class => ['tags'],
            Post::class => ['author'],
        ]);
    }])->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question