Skip to content
Advertisement

How to check array of data to where condition in laravel?

I have a book[] input field that comes from the view

 $arr = $request->book_name;
 dd($arr);

the dd result is like

array:2 [
    0 => "1"
    1 => "3"
]

I want to select data from the library table where book_id match whit the above array, I wrote the below query but it gives an error like:

Type error: Argument 1 passed to IlluminateDatabaseGrammar::parameterize() must be of the type array, string given, called in D

the query is like this:

$libQuantity = DB::table('library')->select('quantity')->whereIn('book_id', '=', $arr)->get();

Advertisement

Answer

Try:

$libQuantity = DB::table('library')
                    ->select('quantity')
                    ->whereIn('book_id', $arr)
                    ->get();

or

$libQuantity = AppModelsLibrary::whereIn('book_id', $arr)
                    ->get(['library']);

The issue is

'book_id', '=', $arr should be 'book_id', $arr

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement