vendredi 4 octobre 2019

Laravel - whenLoaded() and count() on Resource

I have a model for a collection which contains assets.

I wish to return the asset count with the collection resource though as a result this causes the whenLoaded function to be called which as a result loads all of the assets within the collection. I do not wish for this to occur.

I'm not sure how to go about creating a work around which will continue to allow me to use the resource, count the assets, but not call the whenLoaded function.

return [
        'id' => $this->id,
        'name' => $this->name,
        'description' => $this->description,
        'enabled' => $this->enabled,
        'sticky' => $this->sticky,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'asset_count' => $this->assets->where('enabled', 1)->count(),
        'assets' => Asset::collection($this->whenLoaded('assets')),
 ];

Is there a way to still use the resource, return the asset count, but not have the whenLoaded called as a result?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire