Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel DB mysql select where column exists in array?

I send an array to my API made in Laravel 5 which is an array of allowed values ex: [1,3,5]

I try to do the select like this:

$json = (object)Input::all();

$loans = DB::table("loans")
         ->where("years", ">=", $json->year_low)
         ->where("years", "<=", $json->year_high)
         ->where("risk", $json->risks)
         ->get();

risks is the array.

What I receive from the database is and empty array.

In a test I send every possible value 0...4 but I receive an empty array.

How can I select a row which a column's value exists inside an array?

like image 382
Arbitur Avatar asked Jul 01 '15 11:07

Arbitur


2 Answers

If you meant $json->risks is array like [1,2,3], try following

->whereIn("risk", $json->risks)

Read More

like image 124
pinkal vansia Avatar answered Oct 09 '22 14:10

pinkal vansia


I have made some changes in your script :

$json = (object)Input::all();

$loans = DB::table("loans")
     ->where("years", ">=", $json->year_low)
     ->where("years", "<=", $json->year_high)
     ->whereIn("risk", array($json->risks))
     ->get();

This will work and you will get the array of values.

like image 40
Bhavin Solanki Avatar answered Oct 09 '22 14:10

Bhavin Solanki