K
K
kostya17042022-01-08 13:00:23
Laravel
kostya1704, 2022-01-08 13:00:23

How to pass an array of conditions to whereIn in Laravel?

How to pass an array of conditions to whereIn in Laravel?

//Все заказы юзера
$orders = Auth::user()->orders;

//Выборка всех продуктов из всех заказов юзера
$products = DB::table('products')
        ->join('order_product', function ($join) {
            $join->on('products.id', '=', 'order_product.product_id')
       ->whereIn('order_product.order_id', $orders);
        })
        ->get();

I introduced the $orders variable in different forms: array($orders->id ); [$orders->id] ; ran it through foreach.

Such an answer is Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given.

If the array is specified as below , the query works.
->whereIn('order_product.order_id', [12,23,24,56]);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2022-01-08
@kostya1704

->whereIn('order_product.order_id', Auth::user()->orders()->pluck('id'))

I
Ilya, 2022-01-08
@New_Horizons

$orders->pluck('id');
I advise you to still learn php before grabbing frameworks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question