本文介紹了Laravel 4:不存在的地方的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要我的模型只返回一個表中的那些記錄,而另一個表中不存在匹配的記錄.我在想解決方案可能是 Query Scopes 但文檔只是觸及了表面.所以我的 SQL 看起來像這樣:
I need my model to return only those records from one table where a matching record does not exist in another table. I was thinking that the solution might be with Query Scopes but the documentation only scratches the surface. So my SQL would look something like this:
SELECT *
FROM A
WHERE NOT EXISTS (SELECT A_id FROM B
WHERE B.A_id = A.id)
這是我的表格:
A
-------------
| id | name |
-------------
B
--------------------
| id | A_id | name |
--------------------
可能沒有必要,但這是我的模型.A 的模型:
Probably unnecessary but here are my models. Model for A:
class A extends Eloquent{
public function B(){
return $this->hasOne('B', 'A_id', 'id');
}
}
B 模型:
class B extends Eloquent{
public function A(){
return $this->belongsTo('B', 'A_id', 'id');
}
}
推薦答案
類似的東西
A::whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('B')
->whereRaw('A.id = B.id');
})
->get();
這篇關于Laravel 4:不存在的地方的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!