Laravel 多态关系中的预加载正在运行,但 N+1 查询概率仍然存在

Eager Loading in Laravel Polymorphic Relationship is working but N+1 query prob still exist

没有访问相关模型

一切看起来都不错,并且与预加载配合得很好。当没有访问与多态模型相关的模型时。 Everything works well

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

$club->events()->get()->count()

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

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

我的俱乐部控制器

 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');
    }

事件模型

class Event extends Model
{  
    public function sub_events()
    {
        return $this->hasMany('App\Sub_Event', 'event_id');
    }

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

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

    public function club()
    {
        return $this->belongsTo('App\Club');
    }

参考

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

额外 我没有足够的声誉 post 超过 2 张照片来描述我的问题

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

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