A
A
Alexander Ivanov2018-10-08 10:58:05
Laravel
Alexander Ivanov, 2018-10-08 10:58:05

How to pass whereIn through when in laravel?

...
            ->when(request('site_id', false), function ($q, $sites) {
                $q->whereIn('site_id', $sites); // не работает
            })
            ->when(request('href', false), function ($q, $search) {
                $q->where('href', 'like', "%$search%");// работает
            }) ...
        if($sites) {
             $query->whereIn('site_id', $sites); // работает
        }

It's just a desire to transform the code a bit and not to cut everything through if, but when in the when construct does not work out. With what can be connected?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2018-10-08
@cimonlebedev

Because you need to study PHP first - php.net/manual/en/functions.anonymous.php

O
Oleg, 2018-10-08
@402d

Do you have a ruler on your hands to beat for such desires?
the controller decides what to do.
the model provides data.
the view renders.
and you are making a puzzle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question