8

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?

1
  • 2
    whereIn() perhaps? Commented Jul 1, 2015 at 11:30

2 Answers 2

7

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

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

Read More

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.