0

I want to select from the database something like: SELECT * FROM database WHERE id=1 OR id=2 OR id=5;

I do not know the number of the where clauses that the user will request, it can be one, it can be 10.

I tried using Model::orWhere([['id','=',1],['id','=',2],['id','=',5]]) but it seems to be returning an empty list.

In the Laravel documentation, there is this example for the WHERE clause:

users = DB::table('users')->where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

While the WHERE clause it working as in the example, orWhere clause doesn't seem to work like this though.

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42

2 Answers2

2

Use something like:

User::whereIn('id', [1, 2, 3])->get();

As second parameter you need to use an array.

Patryk Woziński
  • 738
  • 1
  • 6
  • 18
2

Following on from my comment you can us an IN Clause could solve this.

$model = Model::whereIn('id', array(1, 2, 3))->get();

Reference How to Make Laravel IN clause

rbaskam
  • 749
  • 7
  • 22