samedi 13 octobre 2018

Laravel query scope in loop

I have a loop that runs a query and builds an associative array, one key/vaue pair for each result set. What is happening is that the scopes keep piling up as the loop iterates.

 foreach($master_asset_categories as $master_category) {
        $master_assets_this_category = $asset_query->group($master_category->id)->get();
        $master_asset_array[$master_category->id] = $master_assets_this_category;
}

the group() scope keeps adding on each loop so it results in something like

group($master_category->id)->group($master_category->id)->group($master_category->id)->group($master_category->id)

with the $master_category->id being different with each loop. That makes the query return nothing since each Asset model has only one asset_group_id and all of the where clauses get chained with "and".

What can I use to remove the latest group() scope after each iteration so there is only the single current group($master_category->id) scope used on each iteration?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire