【发布时间】:2013-05-13 18:18:32
【问题描述】:
我正在尝试使用 2 个 where 子句进行查询,例如:
select * from table1 where `name` = 'paul' AND `id` = 1
在 Laravel 中使用 Eloquent,但我不知道正确的语法。
【问题讨论】:
我正在尝试使用 2 个 where 子句进行查询,例如:
select * from table1 where `name` = 'paul' AND `id` = 1
在 Laravel 中使用 Eloquent,但我不知道正确的语法。
【问题讨论】:
简单,换个where
Model::where('name', '=', 'paul')->where('id', '=', 1);
然后您可以使用get() 或first() 来获取行。
如果您只想使用 Query Builder(Fluent),请将 Model:: 替换为 DB::table('table1')->。
注意
= 在这里是可选的。在这里您可以使用其他运算符。更新
从 Laravel 4.2 你也可以使用数组:
Model::where([
'name' => 'paul',
'id' => 1
]);
【讨论】:
你必须有一个对应于 table1 的对象。
雄辩的对象:
class User extends Eloquent {
protected $table = 'table1';
...
}
ORM 查询:
$user = User::where('name', 'paul')
->where('id', 1)
->first();
【讨论】:
DB::table('table1') 代替 Eloquent 模型。此外,您只需要 where 或 orWhere 进行链接(而不是 andWhere)。