<i id='pFO6L'><tr id='pFO6L'><dt id='pFO6L'><q id='pFO6L'><span id='pFO6L'><b id='pFO6L'><form id='pFO6L'><ins id='pFO6L'></ins><ul id='pFO6L'></ul><sub id='pFO6L'></sub></form><legend id='pFO6L'></legend><bdo id='pFO6L'><pre id='pFO6L'><center id='pFO6L'></center></pre></bdo></b><th id='pFO6L'></th></span></q></dt></tr></i><div id='pFO6L'><tfoot id='pFO6L'></tfoot><dl id='pFO6L'><fieldset id='pFO6L'></fieldset></dl></div>

  1. <small id='pFO6L'></small><noframes id='pFO6L'>

    <tfoot id='pFO6L'></tfoot>
    <legend id='pFO6L'><style id='pFO6L'><dir id='pFO6L'><q id='pFO6L'></q></dir></style></legend>

      • <bdo id='pFO6L'></bdo><ul id='pFO6L'></ul>
    1. Laravel 在关系对象上的位置

      时间:2023-09-24
        <tbody id='aJ8Sv'></tbody>

      <small id='aJ8Sv'></small><noframes id='aJ8Sv'>

            • <bdo id='aJ8Sv'></bdo><ul id='aJ8Sv'></ul>
              <i id='aJ8Sv'><tr id='aJ8Sv'><dt id='aJ8Sv'><q id='aJ8Sv'><span id='aJ8Sv'><b id='aJ8Sv'><form id='aJ8Sv'><ins id='aJ8Sv'></ins><ul id='aJ8Sv'></ul><sub id='aJ8Sv'></sub></form><legend id='aJ8Sv'></legend><bdo id='aJ8Sv'><pre id='aJ8Sv'><center id='aJ8Sv'></center></pre></bdo></b><th id='aJ8Sv'></th></span></q></dt></tr></i><div id='aJ8Sv'><tfoot id='aJ8Sv'></tfoot><dl id='aJ8Sv'><fieldset id='aJ8Sv'></fieldset></dl></div>
              • <tfoot id='aJ8Sv'></tfoot>

                <legend id='aJ8Sv'><style id='aJ8Sv'><dir id='aJ8Sv'><q id='aJ8Sv'></q></dir></style></legend>
                本文介绍了Laravel 在关系对象上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 Laravel 5.0 开发 Web API,但我不确定我正在尝试构建的特定查询.

                I'm developing a web API with Laravel 5.0 but I'm not sure about a specific query I'm trying to build.

                我的课程如下:

                class Event extends Model {
                
                    protected $table = 'events';
                    public $timestamps = false;
                
                    public function participants()
                    {
                        return $this->hasMany('AppParticipant', 'IDEvent', 'ID');
                    }
                
                    public function owner()
                    {
                        return $this->hasOne('AppUser', 'ID', 'IDOwner');
                    }
                }
                

                class Participant extends Model {
                
                    protected $table = 'participants';
                    public $timestamps = false;
                
                    public function user()
                    {
                        return $this->belongTo('AppUser', 'IDUser', 'ID');
                    }
                
                    public function event()
                    {
                        return $this->belongTo('AppEvent', 'IDEvent', 'ID');
                    }
                }
                

                现在,我想获取特定参与者的所有事件.我试过:

                Now, I want to get all the events with a specific participant. I tried with:

                Event::with('participants')->where('IDUser', 1)->get();
                

                但是 where 条件应用于 Event 而不是它的 Participants.以下给了我一个例外:

                but the where condition is applied on the Event and not on its Participants. The following gives me an exception:

                Participant::where('IDUser', 1)->event()->get();
                

                我知道我可以写这个:

                $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.

                使用 Laravel 5 和 Eloquent 通过模型关系执行 where 的最佳方法是什么?

                What is the best way to perform a where through a model relationship using Laravel 5 and Eloquent?

                推荐答案

                对您的关系执行此操作的正确语法是:

                The correct syntax to do this on your relations is:

                Event::whereHas('participants', function ($query) {
                    return $query->where('IDUser', '=', 1);
                })->get();
                

                这将返回参与者的用户 ID 为 1 的事件.如果参与者的用户 ID 不为 1,则不会返回该事件.

                This will return Events where Participants have a user ID of 1. If the Participant doesn't have a user ID of 1, the Event will NOT be returned.

                在 https://laravel.com/docs/5.8/eloquent 阅读更多-relationships#eager-loading

                这篇关于Laravel 在关系对象上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 Eloquent 实现 group_by 并在 Laravel 中拥有 下一篇:Laravel,如何为关系列使用 where 条件?

                相关文章

                最新文章

                  <legend id='1QLcC'><style id='1QLcC'><dir id='1QLcC'><q id='1QLcC'></q></dir></style></legend>
                    <bdo id='1QLcC'></bdo><ul id='1QLcC'></ul>

                  <small id='1QLcC'></small><noframes id='1QLcC'>

                  1. <tfoot id='1QLcC'></tfoot>

                  2. <i id='1QLcC'><tr id='1QLcC'><dt id='1QLcC'><q id='1QLcC'><span id='1QLcC'><b id='1QLcC'><form id='1QLcC'><ins id='1QLcC'></ins><ul id='1QLcC'></ul><sub id='1QLcC'></sub></form><legend id='1QLcC'></legend><bdo id='1QLcC'><pre id='1QLcC'><center id='1QLcC'></center></pre></bdo></b><th id='1QLcC'></th></span></q></dt></tr></i><div id='1QLcC'><tfoot id='1QLcC'></tfoot><dl id='1QLcC'><fieldset id='1QLcC'></fieldset></dl></div>