問題描述
我正在使用 Laravel 并且在使用 Eloquent ORM 時遇到了一個小問題.我可以使用 JOIN 簡單地使用 SQL 查詢來實現它,但我似乎無法讓它與 Eloquent 一起使用!
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!
這就是我想要的,我有兩張表.一個是Restaurants",另一個是Restaurant_Facilities".
This is what I want, I have two tabels. one is 'Restaurants' and other is 'Restaurant_Facilities'.
表格很簡單……而且是一對一的關系.就像有一個帶有 id
、name
、slug
等的 restaurant
表和另一個名為 restaurant_facilities 的表
帶有 id
、restaurant_id
、wifi
、parking
等
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
現在我想做的是..加載所有wifi = 1或wifi = 0的餐廳..我怎么能用 Eloquent 做到這一點?我嘗試過急切加載、數據透視表、with()、collections(),但似乎沒有任何效果!
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!
對于cuisines
的多對多關系,我遇到了同樣的問題!我有相同的 restaurant
表和一個 cuisine
表和一個 restaurant_cuisine_connection
表..
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..
但是如何使用 ID 將所有餐廳加載到特定美食中?
but how do I load all restaurants inside a specific cuisine using it's ID ?
這行得通.
Cuisine::find(6)->restaurants()->get();
但我想從 Restaurant:: 模型加載它而不是從菜系中加載..因為我有很多條件鏈接在一起..它用于搜索和過濾/瀏覽頁面.
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.
任何想法或方法?我已經為此苦苦掙扎了 3 天,但仍然沒有答案.
Any ideas or ways ? I've been struggling with this for 3 days and still no answer.
示例模型:
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();
還有什么是在沒有其他查詢的情況下找到具有特定列值的餐館數量的最佳方法?比如..我必須找到有停車位 = 1 和 wifi = 1 的餐廳總數?
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 ?
請幫忙解決這個問題.
謝謝.
推薦答案
如果您必須從 Restaurant 模型加載,我認為在此處執行左連接沒有任何問題.我可能會將它抽象為我的 Restaurant 模型上的一個方法,如下所示:
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);
}
}
然后,從您的路線:
Route::get('/', function()
{
return Restaurant::withWifi()->get();
});
在旅途中 - 尚未測試該代碼,但我認為它應該可以工作.您可以改為使用帶有約束的預加載,但這只會指定工具對象是否為空.除非您指定 where 子句,否則它仍會返回所有餐廳.
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. 我會堅持使用 Facility 的單數形式.注意 hasOne('Facilities')
怎么讀不正確?)
(P.S. I'd stick with the singular form of Facility. Notice how hasOne('Facilities')
doesn't read correctly?)
這篇關于Laravel,如何為關系列使用 where 條件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!