0

Is the following query possible with Eloquent ORM without using whereRaw():

SELECT *
FROM Users
WHERE name="John"
OR name="Bill";

This is the exact problem that I have. I need to find a certain set of specific values from a certain column that is NOT the primary key (and so I cannot use find()).

However, I would be interested in the more general OR clause:

SELECT *
FROM Users
WHERE name="John"
OR age="26";

in which you can put any number OR clauses together, regardless of their content.

As I understand it, chaining multiple ->where()s will connect them using AND. I.e.:

User::where("name", "=", "John")
  -> where("name", "=", "Bill)
  -> get();

produces

SELECT *
FROM Users
WHERE name="John"
AND name="Bill";

Feel free to correct me if I'm wrong.

ewok
  • 20,148
  • 51
  • 149
  • 254
  • 2
    possible duplicate of [Laravel 4 eloquent WHERE with OR AND OR?](http://stackoverflow.com/questions/16995102/laravel-4-eloquent-where-with-or-and-or) – mike.bronner Aug 03 '14 at 02:12

1 Answers1

3

What you need is orWhere. From the Laravel docs

$users = DB::table('users')
             ->where('votes', '>', 100)
             ->orWhere('name', 'John')
             ->get();
Christian South
  • 359
  • 2
  • 9
  • is this really using Eloquent, though? It's a perfectly fine solution, and probably what I will use, but it doesn't really seem in keeping with the nature of the ORM – ewok Aug 03 '14 at 02:28
  • @ewok Eloquent has both the where and orWhere methods. So the code above would work just as well if you replace DB::table('users') with User::where('name', '=', 'John')->orWhere('name', '=', 'Bill'); – Christian South Aug 03 '14 at 02:54