php Laravel 在关系对象上的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29989908/
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 where on relationship object
提问by Lic
I'm developing a web API with Laravel 5.0 but I'm not sure about a specific query I'm trying to build.
我正在使用 Laravel 5.0 开发 Web API,但我不确定要构建的特定查询。
My classes are as follows:
我的课程如下:
class Event extends Model {
protected $table = 'events';
public $timestamps = false;
public function participants()
{
return $this->hasMany('App\Participant', 'IDEvent', 'ID');
}
public function owner()
{
return $this->hasOne('App\User', 'ID', 'IDOwner');
}
}
and
和
class Participant extends Model {
protected $table = 'participants';
public $timestamps = false;
public function user()
{
return $this->belongTo('App\User', 'IDUser', 'ID');
}
public function event()
{
return $this->belongTo('App\Event', 'IDEvent', 'ID');
}
}
Now, I want to get all the events with a specific participant. I tried with:
现在,我想获取特定参与者的所有事件。我试过:
Event::with('participants')->where('IDUser', 1)->get();
but the where
condition is applied on the Event
and not on its Participants
. The following gives me an exception:
但where
条件适用于Event
而不适用于它的Participants
。以下给了我一个例外:
Participant::where('IDUser', 1)->event()->get();
I know that I can write this:
我知道我可以这样写:
$list = Participant::where('IDUser', 1)->get();
for($item in $list) {
$event = $item->event;
// ... other code ...
}
but it doesn't seem very efficient to send so many queries to the server.
但是向服务器发送这么多查询似乎不是很有效。
What is the best way to perform a where
through a model relationship using Laravel 5 and Eloquent?
where
使用 Laravel 5 和 Eloquent执行模型关系的最佳方法是什么?
回答by Crembo
The correct syntax to do this on your relations is:
对您的关系执行此操作的正确语法是:
Event::whereHas('participants', function ($query) {
$query->where('IDUser', '=', 1);
})->get();
Read more at https://laravel.com/docs/5.8/eloquent-relationships#eager-loading
在https://laravel.com/docs/5.8/eloquent-relationships#eager-loading阅读更多
回答by Hamid Naghipour
@Cermbo's answer is not related to this question. In their answer, Laravel will give you all Events
if each Event
has 'participants'
with IdUser
of 1
.
@Cermbo 的回答与这个问题无关。在他们的回答,Laravel会给大家Events
如果每个Event
都有'participants'
用IdUser
的1
。
But if you want to get all Events
with all 'participants'
provided that all 'participants'
have a IdUser
of 1, then you should do something like this :
但是如果你想得到所有的东西Events
,'participants'
只要所有的'participants'
都IdUser
为 1,那么你应该做这样的事情:
Event::with(["participants" => function($q){
$q->where('participants.IdUser', '=', 1);
}])
N.B:
注意:
in where
use your table name, not Model name.
在where
使用你的表名,而不是型号名称。
回答by Brandon Stewart
return Deal::with(["redeem" => function($q){
$q->where('user_id', '=', 1);
}])->get();
this worked for me
这对我有用
回答by Muhammad Shahzad
With multiple joins, use something like this code:
对于多个连接,请使用以下代码:
Event::with(["owner", "participants" => function($q) use($someId){
$q->where('participants.IdUser', '=', 1);
//$q->where('some other field', $someId);
}])