samedi 24 novembre 2018

eager load and empty queries

I'm trying to put my catalog in cache by team. Price may vary by team. But if the price is not set for a product I need to send the "default" team value", and can't find an easy way to do this.

$teams = Team::all();

foreach ($teams as $team) {

    $teamId = $team->id;

    $catalog = Cache::rememberForever('catalog-' . $team->id, function () use ($teamId) {

        $catalog = Category::with(['products.prices' => function ($q) use ($teamId) {
            $q->where("team_id", $teamId); // and if no result $q->where("team_id",1);
        }])->all();

        return $catalog;
    });
}

I was searching with queries like $q->whereNotExist, but I can't find a way to do it correctly. They may be some other way to do this via Product or Price model (and mutator), but I also stuck here.

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire