简体   繁体   中英

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.

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
             ]);

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM