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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:41:44  来源:igfitidea点击:

Laravel where on relationship object

phplaraveleloquentrelationshipwhere-clause

提问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 wherecondition is applied on the Eventand 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 wherethrough 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 Eventsif each Eventhas 'participants'with IdUserof 1.

@Cermbo 的回答与这个问题无关。在他们的回答,Laravel会给大家Events如果每个Event都有'participants'IdUser1

But if you want to get all Eventswith all 'participants'provided that all 'participants'have a IdUserof 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 whereuse 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);
}])