Closed f4xy closed 2 years ago
You are always welcome,
For sure you can eager load the relations, simply, add the relation you want to, in your case (as far as I understand it) it must be 'likers'
So please add it to your with
function or attribute of your model
Let me know if it worked or if you need any further help
The problem is not solved.
I have added the preload function.
protected $with = ['user', 'replies', 'likers']
in Models/Comment
;
Can you use withCount
or morphWithCount
in the package you develop?
The query data is too much, the reason why the EAGER function fails is that I used $model->likers()->count()
{{ $model->likers()->count() }}
this will always cause N + 1 because the ->likers()
is a new query instance.
the prelaoded items will always work. but instead using the ->count()
method you should use it like this count( $model-> likers)
.
but there is a cleaner way to do this.
eg.
return view('livewire.comment.comments', [
'comments' => $this->model->comments()->withCount(['likers'])->paginate(10);
]);
-------------------------------------------------------------------------------------------
to explain:
$model = $this->model->comments()->withCount(['likers'])->paginate(10);
and so
// model should be looped in your case, this is to very the first item has the likers count
$item = $model[0];
$item->likers_count; // This is the eager loaded liker count.
and with replies() you can always add another methods on the relation.
return $this->hasMany(Comment::class, 'parent_id')->withCount(['likers'])->oldest();
Models/Thread.php ` class Thread extends Model { use HasFactory;
} `
Models/Comment.php ` class Comment extends Model { use HasFactory, CanBeLiked;
} `
Livewire/Comments.php `class Comments extends Component { public $model;
}`
Livewire/comment.php ` class Comment extends Component { public $comment;
} `
When I call {{$model - > likers() - > count()}} on the front end, the N + 1 problem occurs.
I use ‘beyondcode/laravel-query-detector’ package to remind me:
and debugbar :
Thank you very much for providing such a powerful package, and I hope you can help me. This problem has bothered me for a week, but it still can't be solved. Thank you very much again!