Laravel,如何使用关系列的条件? [英] Laravel, How to use where conditions for relation's column?
问题描述
这是我想要的,我有两个表格。一个是餐厅等餐厅功能。
表格简单,一一对应。喜欢有一个餐厅
表与 id
,名称
slug
等,另一个名为 restaurant_facilities
的表与 id
restaurant_id
, wifi
,停车
等
现在我想做的是..加载所有有wifi = 1或wifi = 0的餐馆。
我怎么能用雄辩才能做到这一点?我已经尝试加载,数据透视表,with(),collections(),似乎没有任何工作!
同样的问题我有一个多对多的关系对于 cuisines
!
我有相同的餐厅
表和美食
表和 restaurant_cuisine_connection
table ..
但是如何使用其ID加载特定菜肴中的所有餐厅?
$ b $
Cuisine :: find(6) - > restaurants() - > get();
但是我想从餐厅::模型中加载这个不是美食,因为我有很多条件链接在一起..它的搜索和过滤/浏览页面。
任何想法或方式?我一直在努力3天,仍然没有回答。
示例模型:
class Restaurant Extended Enloquent {
protected $ table ='restaurants';
public function facilities(){
return $ this-> hasOne('Facilities');
}
}
类设施扩展Eloquent {
protected $ table ='restaurants_facilities';
public function restaurant(){
return $ this-> belongsTo('Restaurant');
}
}
PS:
This似乎正在工作..但这不是很好的方式吗?
餐厅:: leftJoin(
'cuisine_restaurant' ,
'cuisine_restaurant.restaurant_id',
'=','restaurants.id'
)
- >其中('cuisine_id',16)
- > ;得到();
还有什么最好的方法来找到没有另一个查询的具有特定列值的餐馆数?我必须找到有停车位= 1和wifi = 1的餐馆总数。
请帮忙。
谢谢。
如果你在左边加入,我没有看到任何错误必须从餐厅模型加载。我可以将它抽象出来,在我的餐厅模型中使用一种方法:
class Restaurant扩展Eloquent {
protected $ table ='restaurants'; //将在最新的L4 beta
中的默认值public function facility()
{
return $ this-> hasOne('Facility');
}
//或者,更好的是让public,并向控制器注入实例。
public static function withWifi()
{
return static :: leftJoin(
'restaurant_facilities',
'restaurants.id','=','restaurant_facilities。 restaurant_id'
) - > where('wifi','=',1);
}
}
然后,从你的路线:
Route :: get('/',function()
{
return Restaurant :: withWifi() - > ; get();
});
在旅途中 - 没有测试过该代码,但我认为它应该有效。您可以使用强制加载约束,但这只会指定设施对象是否为空。它仍然会返回所有的餐馆,除非你指定一个where子句。
(PS我会坚持使用单一形式的Facility。注意如何 hasOne('Facilities')
不正确读取?)
I'm using Laravel and having a small problem with Eloquent ORM.. I can get this working simply with SQL query using a JOIN but I can't seem to get it working with Eloquent!
This is what I want, I have two tabels. one is 'Restaurants' and other is 'Restaurant_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 restaurant_facilities
with id
, restaurant_id
, wifi
, parking
, etc
Now what I want to do is.. load all restaurants which have wifi = 1 or wifi = 0.. How can i do that with Eloquent ? I have tried eager loading, pivot tables, with(), collections() and nothing seems to work!
The same problem I have for a Many-To-Many relation for cuisines
!
I have the same restaurant
table and a cuisine
table and a restaurant_cuisine_connection
table..
but how do I load all restaurants inside a specific cuisine using it's ID ?
This works.
Cuisine::find(6)->restaurants()->get();
but I wanna load this from Restaurant:: model not from cuisines.. because I have many conditions chained together.. its for a search and filtering / browse page.
Any ideas or ways ? I've been struggling with this for 3 days and still no answer.
Example Models :
class Restaurant extends Eloquent {
protected $table = 'restaurants';
public function facilities() {
return $this->hasOne('Facilities');
}
}
class Facilities extends Eloquent {
protected $table = 'restaurants_facilities';
public function restaurant() {
return $this->belongsTo('Restaurant');
}
}
PS : This seems to be working.. but this is not Eloquent way right ?
Restaurant::leftJoin(
'cuisine_restaurant',
'cuisine_restaurant.restaurant_id',
'=', 'restaurants.id'
)
->where('cuisine_id', 16)
->get();
Also what is the best method to find a count of restaurants which have specific column value without another query ? like.. i have to find the total of restaurants which have parking = 1 and wifi = 1 ?
Please help on this.
Thank you.
I don't see anything wrong with doing the left join here, if you have to load from the Restaurant model. I might abstract it away to a method on my Restaurant model, like so:
class Restaurant extends Eloquent {
protected $table = 'restaurants'; // will be default in latest L4 beta
public function facility()
{
return $this->hasOne('Facility');
}
// Or, better, make public, and inject instance to controller.
public static function withWifi()
{
return static::leftJoin(
'restaurant_facilities',
'restaurants.id', '=', 'restaurant_facilities.restaurant_id'
)->where('wifi', '=', 1);
}
}
And then, from your routes:
Route::get('/', function()
{
return Restaurant::withWifi()->get();
});
On the go - haven't tested that code, but I think it should work. You could instead use eager loading with a constraint, but that will only specify whether the facility object is null or not. It would still return all restaurants, unless you specify a where clause.
(P.S. I'd stick with the singular form of Facility. Notice how hasOne('Facilities')
doesn't read correctly?)
这篇关于Laravel,如何使用关系列的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!