Ruben
Ruben

Reputation: 454

Create SQL query with more than one where clause

I'm trying to do a query with 2 where clauses like:

select * from table1 where `name` = 'paul' AND `id` = 1

in Laravel with Eloquent, but I don't know the correct syntax.

Upvotes: 2

Views: 5724

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 12503

Simple, use another where

Model::where('name', '=', 'paul')->where('id', '=', 1);

Then you may use get() or first() to fetch the row(s).

If you want to use just Query Builder(Fluent) then replace Model:: with DB::table('table1')->.

Note

  • = is optional here. Here you can use other operators.

Update

From Laravel 4.2 you can also use array:

Model::where([
               'name' => 'paul', 
               'id' => 1
             ]);

Upvotes: 11

Rok Burgar
Rok Burgar

Reputation: 949

You have to have an object that corresponds to table1.

Eloquent object:

class User extends Eloquent {

    protected $table = 'table1';

    ...
}

ORM query:

$user = User::where('name', 'paul')
              ->where('id', 1)
              ->first();

Upvotes: 2

Related Questions