You are saying that Event and History has a relationship with User, but you are trying to load the relationship on Notification?
Hi Herlevsen, i'm gonna try to explain a little bit deeper
Yes Event and History both 'belongs_to' User.
It starts with :
$notifications = Notification::orderBy('created_at')->limit(50)->with('notifiable');
in a controller. Then I call a view in which there is something like:
<ul>
@foreach($notifications as $notification)
<li>
@include('notifications._show'.get_class($notification->notifiable))
</li>
@endforeach
</ul>
Meaning i have as many partials views as there are models 'notifiable' ( in my case _showEvent and _showHistory ) In a perfect world, inside, for example _showHistory partial i would have liked to write
{{ $notification->notifiable->user->name }} has an item named {{ $notification->notifiable->item->name }}
This actually works but you can't eager load it, it creates too many requests. So i had to eager load it myself, put things in array myself and i do things like :
{{ $userArray[$notification->notifiable->user_id]->name }}
has an item named {{ $itemArray[$notification->notifiable->item_id]->name }}
That works okay but it's not very elegant, plus it makes reusability of my partials highly doubtable. But now it's hell faster .....
Hope this helps
You can segregate each type of notification into their own collections and then eager load on the separate collections. I think that should eager load your original collection because of PHP's object by reference.
Try it out.
I'm curious as to the content of your notifiable() method on the Notification model and how you're able to even get a mixed collection in the first place. Can you post that?
Have you tried looking into using $with on the model?
protected $with = ['user'];
Just throwing out ideas.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community