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

    1. <legend id='Jl2oy'><style id='Jl2oy'><dir id='Jl2oy'><q id='Jl2oy'></q></dir></style></legend>

      1. <tfoot id='Jl2oy'></tfoot>
      2. <small id='Jl2oy'></small><noframes id='Jl2oy'>

        • <bdo id='Jl2oy'></bdo><ul id='Jl2oy'></ul>

        laravel 4 雄辩的急切加载关系计数

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

          • <legend id='DirmZ'><style id='DirmZ'><dir id='DirmZ'><q id='DirmZ'></q></dir></style></legend>

          • <small id='DirmZ'></small><noframes id='DirmZ'>

                  <tbody id='DirmZ'></tbody>

                  <tfoot id='DirmZ'></tfoot>
                  本文介绍了laravel 4 雄辩的急切加载关系计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个具有多个定义关系的复杂模型.在这个例子中,我想计算 Like 模型并创建一个名为 likes 的属性,以便它可以从 REST 服务返回.

                  I have a complex Model with multiple defined relations. In this example I would want to count the Like model and create a property named likes so it can be returned from a REST service.

                  是否可以将模型计数预先加载到动态属性中?

                  Is it possible to eager load a model count into a dynamic property?

                  $beat = Post::with(
                           array(
                              'user',
                              'likes' => function($q){
                                  $q->count();
                              }
                          ))
                          ->where('id', $id)
                          ->first();
                  

                  推荐答案

                  假设您有 Post->hasMany->Like 关系,并且您已声明 Likes 关系为:

                  Assuming you are having Post->hasMany->Like relationship and you have declared likes relationship as:

                  class Post{
                  
                    public function likes(){
                     return $this->hasMany('Like');
                    }
                  }
                  

                  创建一个新函数,比如 likeCountRelation 为:

                  create a new function say likeCountRelation as:

                  public function likeCountRelation()
                  {
                      $a = $this->likes();
                  
                      return $a->selectRaw($a->getForeignKey() . ', count(*) as count')->groupBy($a->getForeignKey());
                  }
                  

                  现在您可以将 __get() 函数重写为:

                  now you can override __get() function as:

                  public function __get($attribute)
                  {
                  
                      if (array_key_exists($attribute, $this->attributes)) {
                          return $this->attributes[$attribute];
                      }
                  
                      switch ($attribute) {
                  
                          case 'likesCount':
                              return $this->attributes[$attribute] = $this->likesCountRelation->first() ? $this->likesCountRelation->first()->count : 0;
                              break;
                  
                          default:
                              return parent::__get($attribute);
                  
                      }
                  }
                  

                  或者你可以使用 getattribute 函数:

                  or you can use getattribute function as :

                  public function getLikesCountAttribute(){
                   return $this->likesCountRelation->first() ? $this->likesCountRelation->first()->count : 0;
                  }
                  

                  并且只需将 likesCount 作为 $post->likesCount 访问,您甚至可以像这样急切加载它:

                  and simply access likesCount as $post->likesCount you can even eager load it like:

                  $posts=Post::with('likesCountRelation')->get();
                   foreach($post as $post){
                    $post->likesCount;
                   }
                  

                  注意:相同的逻辑可用于变形许多关系.

                  NOTE: Same logic can be used for morph many relationships.

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

                  上一篇:Laravel Carbon Group 按月 下一篇:Laravel eloquent - 在加入表时防止覆盖值

                  相关文章

                  最新文章

                  1. <legend id='7SMwi'><style id='7SMwi'><dir id='7SMwi'><q id='7SMwi'></q></dir></style></legend>

                    <tfoot id='7SMwi'></tfoot>
                      <bdo id='7SMwi'></bdo><ul id='7SMwi'></ul>

                      <small id='7SMwi'></small><noframes id='7SMwi'>

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