在Laravel多态关系中的急切加载正在起作用,但N + 1查询问题仍然存在

时间:2016-06-01 04:57:44

标签: laravel eloquent polymorphism laravel-5.2 eager-loading

无访问相关模型

一切看起来都很好,并且在充满热情的情况下运作良好。无法访问与多态模型相关的模型时。 Everything works well

但是当我尝试访问其他模型时

$俱乐部 - >事件() - >得到() - >计数()

a := []string{"A", "B"}
b := a
fmt.Printf("%v\n", a)
fmt.Printf("%v\n", b)

b[0] = "C"
fmt.Printf("%v\n", a)
fmt.Printf("%v\n", b)
// a has been changed.

This is my database structure. The Records table will record all the participant from Event/Branch Event/Club

我的clubController

<tbody>
                            @foreach($Clubs as $club)
                                <tr>
                                    <td><a href="{{url('database/club/').'/'.$club->id}}">{{$club->club_name}}</a>
                                    </td>
                                    <td>{{$club->origin}}</td>
                                    <td>{{$club->hp}}</td>
                                    <td>{{$club->email}}</td>
                                    <td>{{$club->location}}</td>
                                    <td>{{$club->category}}</td>
                                    <td>{{$club->rating}}</td>
                        //This cause N+1 problem to happen
                                    <td>{{$club->events()->get()->count()}}</td>

                       //I will get more problem when access more!!!!!
                                    {{--<td>{{$club->records()->get()->count()}}</td>--}}

                                </tr>
                            @endforeach
</tbody>

记录模型

 public function index()
{
    $Clubs = $this->club->with(['records','records.recordable', 'events'])->get();

    return view('pages.database.club', compact(['Clubs']));
}

分会模特

    class Record extends Model
{

    public function recordable()
    {
        return $this->morphTo();
    }

    public function events()
    {
        return $this->morphedByMany('App\Event', 'recordable', 'records', 'recordable_id');
    }

    public function sub_events()
    {
        return $this->morphedByMany('App\Sub_Event', 'recordable', 'records', 'recordable_id');
    }

    public function clubs()
    {
        return $this->morphedByMany('App\Club', 'recordable', 'records', 'recordable_id');
    }


    public function user()
    {
        return $this->belongsTo('App\User', 'user_id');
    }

活动模型

class Club extends Model
{
    public function records()
    {
        return $this->morphMany('App\Record','recordable');
    }

    // public function users()
    // {
    //     return $this->belongsToMany('App\User','club_user');
    // }
    public function events()
    {
        return $this->hasMany('App\Event');
    }

参考

我尝试通过答案进行搜索,但它似乎总是过时了。我尝试了解决方案 http stackoverflow.com/questions/26727088/laravel-eager-loading-polymorphic-relations-related-models 但仍然无法解决这个问题。

附加 我没有足够的声誉来发布超过2张照片来描述我的问题

1 个答案:

答案 0 :(得分:1)

您访问加载的关系时出错了。执行$club->events()->get()->count()时,您要求$club模型从数据库中再次获取关系,而不是尝试获取已经在急切加载中加载的关系。

正确的方法是将这两行更改为:$club->events->count()