mercredi 21 mars 2018

Retrieve model from notification

With Laravel, I can create database notifications.

Assuming, my notifications are about Resources (a model named Resource), I can give a resource instance to the notification constructor:

public function __construct(Resource $resource)
{
    $this->resource = $resource;
}

// ... 

public function toArray($notifiable)
{
    return [
        'resource_id' => $this->resource->id
    ];
}

I would like to load notifications for a user and display some resources information. I can get all notifications and do something like this:

foreach ($user->notifications as $notification) {

    $resource_id = $notification->resource_id;
    $resource = Resource::find($resource_id);

    // Do something with resource information...
    echo $resource->name;

}

But it's slow (one query for each notification) and not à la Laravel. I would prefer something like:

foreach ($user->notifications as $notification) {

    // Do something with resource information...
    echo $notification->resource->name;

}

Ideally, it would like to have some kind of eager loading. Is there an elegant (or at least working) way to do this?



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

Aucun commentaire:

Enregistrer un commentaire