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

    <small id='4xCFU'></small><noframes id='4xCFU'>

    <legend id='4xCFU'><style id='4xCFU'><dir id='4xCFU'><q id='4xCFU'></q></dir></style></legend>
    <tfoot id='4xCFU'></tfoot>

    1. Laravel 中关系的计数关系

      时间:2023-09-22
        <i id='94uxk'><tr id='94uxk'><dt id='94uxk'><q id='94uxk'><span id='94uxk'><b id='94uxk'><form id='94uxk'><ins id='94uxk'></ins><ul id='94uxk'></ul><sub id='94uxk'></sub></form><legend id='94uxk'></legend><bdo id='94uxk'><pre id='94uxk'><center id='94uxk'></center></pre></bdo></b><th id='94uxk'></th></span></q></dt></tr></i><div id='94uxk'><tfoot id='94uxk'></tfoot><dl id='94uxk'><fieldset id='94uxk'></fieldset></dl></div>
        <tfoot id='94uxk'></tfoot>

        <small id='94uxk'></small><noframes id='94uxk'>

          <bdo id='94uxk'></bdo><ul id='94uxk'></ul>

                <legend id='94uxk'><style id='94uxk'><dir id='94uxk'><q id='94uxk'></q></dir></style></legend>

                  <tbody id='94uxk'></tbody>

                本文介绍了Laravel 中关系的计数关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                假设我有一个像这样的 Conversation 模型:

                Suppose I have a Conversation model like this :

                class Conversation extends Model
                {
                    public function questions (){
                        return $this->hasMany('AppQuestion','conversation_id','conversation_id');
                    }
                    public function category ()
                    {
                        return $this->belongsTo('AppCategory', 'cat', 'cat_id');
                    }
                
                }
                

                还有一个像这样的 Question 模型:

                And a Question model like this:

                class Question extends Model
                {
                    public function conversation ()
                    {
                        return $this->belongsTo('AppConversation', 'conversation_id', 'conversation_id');
                    }
                }
                

                如您所见,这两者之间存在 hasMany 关系.

                As you can see there is a hasMany relation between those two.

                另一方面,有一个像下面这样的 CategoryConversation 模型有关系:

                In the other hand there is a Category like below that has a relation with Conversation model :

                class Category extends Node
                {
                    public function conversations (){
                        return $this->hasMany('AppConversation','cat','cat_id');
                    }
                }
                

                现在我想将一个名为 question_count 的属性附加到 Category 来计算每个类别的对话的所有问题.为此,我添加了这个:

                Now I want to append an attribute named question_count to Category that counts all questions of conversations of each category. for that I added this :

                    public function getQuestionsCountAttribute ()
                    {
                        return $this->conversations->questions->count();
                    }
                

                但是在获取类别时出现此错误:

                But when fetch a category I got this error :

                ErrorException in Category.php line 59:
                Undefined property: IlluminateDatabaseEloquentCollection::$questions
                

                我做了什么?如何在最小服务器过载的情况下计算关系的关系?

                What did I do? how can I count relations of a relation with minimum server overloading?

                我使用的是 Laravel 5.3.4.

                I am using laravel 5.3.4.

                推荐答案

                我认为这里你需要一个有很多的关系.

                I think that you need a has many through relationship here.

                你做错了什么:

                当你写$this->conversations->questions时,这是行不通的,因为questions单个对话的关系 而不是对话的集合(这里,$this->conversations 是一个集合)

                When you write $this->conversations->questions, this can't work, because the questions are a relation of a single conversation and not of a collection of conversations (here, $this->conversations is a Collection)

                解决办法:

                使用 hasManyThrough 关系:

                Using hasManyThrough relation:

                您可以在在此页面上找到有关此关系的文档,如果我的解释不好

                You can find the documentation for this relation on this page, if my explanation is bad

                基础是,你需要在你的 Category 模型上定义一个关系:

                The basics are, you need to define a relation on your Category model:

                class Category extends Node
                {
                    public function conversations ()
                    {
                        return $this->hasMany('AppConversation');
                    }
                
                    public function questions ()
                    {
                        return $this->hasManyThrough('AppQuestion', 'AppConversation');
                    }
                }
                

                (我会让你查看非标准外键的文档)

                (I will let your look into the documentation for your non standards foreign keys)

                然后你应该可以使用:$category->questions->count()

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

                上一篇:Laravel 获取每个组的最新记录 下一篇:Laravel 获取祖先(URL)

                相关文章

                最新文章

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

                  <small id='3eT9q'></small><noframes id='3eT9q'>