Support the ongoing development of Laravel.io →
Database Eloquent

I have a Notification which implements

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

public function receiver()
    {
        return $this->belongsTo('User');
    }

public function sender()
    {
        return $this->belongsTo('User');
    }

And different classes (Comment for ex)

public function notifications()
    {
        return $this->morphMany('Notification', 'notifiable');
    }

public function answer()
    {
        return $this->belongsTo('Answer');
    }

public function commenter()
    {
        return $this->belongsTo('User', 'user_id');
    }
...

Using the NotificationRepository I would like to eger load the answers for each comment

public function paged()
    {
        $rows = $this->model
            ->with('notifiable.answer.report.user')
            ->latestForUser($this->user->id)
            ->simplePaginate($this->perPage);

        return $rows;
    }

The above code won't eger load the whole relationship tree and I end up with A LOT of queries for each user, report and answer when listing each notification (in notifications.index)

What am I doing wrong?

Last updated 3 years ago.
0

Anyone, anything? :)

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.