mercredi 28 juin 2017

Can I use `when` with `whereHas` in a Laravel query?

I want to add a whereHas to a Laravel query but only if a variable is true. I'm trying the below which doesn't error but doesn't return anything when it should.

$assets = Asset::with('media')
            ->when($category, function ($q) use ($category) {
                return $q->whereHas('category', function ($query) use ($category) {
                    $query->whereId($category);
                });
            })->offset($offset)->limit($limit)->get();

If $category is true nothing is returned. If $category is false all is returned. It works without the when clause.



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

Aucun commentaire:

Enregistrer un commentaire