Ricardo
6 months ago

loadMorph a distant relation

Posted 6 months ago by Ricardo

Using docs example:

use Illuminate\Database\Eloquent\Model;

class ActivityFeed extends Model
{
    /**
     * Get the parent of the activity feed record.
     */
    public function parentable()
    {
        return $this->morphTo();
    }
}

and then:

$activities = ActivityFeed::with('parentable')
    ->get()
    ->loadMorph('parentable', [
        Event::class => ['calendar'],
        Photo::class => ['tags'],
        Post::class => ['author'],
    ]);

Now to the question. Given a:

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the activities of a user.
     */
    public function activities()
    {
        return $this->hasMany(ActivityFeed::class);
    }
}

How to loadMorph?

Something like $users->loadMorph('activities.parentable', ...);

Thanks for the time.

Please sign in or create an account to participate in this conversation.