Laravel 4 - 如何使用关系列的 where 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18885127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - How to use where conditions for relation's column
提问by Hilton Lam
This is what I want, I have two tables. one is 'Restaurants' and other is 'Facilities'.
这就是我想要的,我有两张桌子。一个是“餐厅”,另一个是“设施”。
The tables are simple.. and One-To-One relations. like there is a restaurant table with id
, name
, slug
, etc and another table called facilities
with id
, restaurant_id
, wifi
, parking
, etc
表格很简单......和一对一的关系。像有一个餐厅桌子id
,name
,slug
,等和另一个表称为facilities
有id
,restaurant_id
,wifi
,parking
,等
Here are my models:
这是我的模型:
class Restaurant extends Eloquent {
protected $table = 'restaurants';
public function facilities() {
return $this->hasOne('Facilities');
}
}
class Facilities extends Eloquent {
protected $table = 'facilities';
public function restaurant() {
return $this->belongsTo('Restaurant');
}
}
I want do like this Select * from restaurants r left join facilities rf on r.id=rf.restaurant_id where r.name = 'bbq' and rf.wifi != '1'
.
我想这样做Select * from restaurants r left join facilities rf on r.id=rf.restaurant_id where r.name = 'bbq' and rf.wifi != '1'
。
How to use Eloquent to do that?
如何使用 Eloquent 做到这一点?
ps. sorry for modify from https://stackoverflow.com/questions/14621943/laravel-how-to-use-where-conditions-for-relations-column#=, but I have the similar problem.
附:抱歉从https://stackoverflow.com/questions/14621943/laravel-how-to-use-where-conditions-for-relations-column# =修改,但我有类似的问题。
回答by fideloper
You can use where
and other sql-based methods on the relationship objects.
您可以where
在关系对象上使用和其他基于 sql 的方法。
That means you can either create a custom method in your model:
这意味着您可以在模型中创建自定义方法:
class Restaurant extends Eloquent {
protected $table = 'restaurants';
public function facilities($wifi) {
return $this->belongsTo('Facility')->where('wifi', '=', $wifi);
}
}
Or you can try to use query scopes:
或者您可以尝试使用查询范围:
class Restaurant extends Eloquent {
protected $table = 'restaurants';
public function facility() {
return $this->belongsTo('Restaurant');
}
public function scopeFiltered($query, $wifi)
{
return $query->where('wifi', '>', 100);
}
}
Then:
然后:
$wifi = 1;
$restaurants = Restaurant::facilities()->filtered($wifi)->get();
This isn't exactly what you need likely, but query scopes is likely what you want to use to get what you're attempting.
这可能不是您真正需要的,但查询范围可能是您想要用来获取您正在尝试的内容的范围。
THe key point is to know that relationship classes can be used like query builders - for example:
关键是要知道关系类可以像查询构建器一样使用 - 例如:
$this->belongsTo('Facility')->where('wifi', '=', $wifi)->orderBy('whatever', 'asc')->get();
回答by Antonio Carlos Ribeiro
There are some ways to filter both, this is using QueryBuilder:
有一些方法可以过滤两者,这是使用 QueryBuilder:
Restaurant::join('facilities','facilities.restaurant_id','=','restaurants.id')
->where('name','bbq')
->where('facilities.wifi','!=', 1)
->get();