Laravel DB mysql choose where column exists in array?

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

I am trying to make a selection as follows:

 $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 an array.

What I get from the database, and an empty array.

In the test, I send all possible values ​​0 ... 4, but I get an empty array.

How to select a row whose column value exists in an array?

+6
source share
2 answers

If you meant $json->risks - an array like [1,2,3] , try to run

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

More details

+6
source

I made some changes to 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 an array of values.

+2
source

All Articles