本文介绍了限制Laravel中嵌套关系的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要设置条件和限制的嵌套关系.
I have a nested relationship that I want to set a condition and a limit.
$data = Accommodation::with('accommodationFacilities', 'city')
->take(10)
->with('accommodationRooms.roomPricingHistory')
->limit(2)
->where('is_deleted', 0)
->paginate(10);
现在我在第2行有一个嵌套关系,我想通过以下方式限制该关系: roomPricingHistory
.
Now I have a nested relation on line 2 that I want to limit this relation by: roomPricingHistory
.
该代码限制了父关系,即: accommodationRooms
.
The code limits the parent relation which is: accommodationRooms
.
任何想法如何限制子关系以及如何为其设置if,因此如果列=== 0,则应加载该行.
Any idea how I can limit the child relation and how can I set an if for it so if a column is === 0 this row should be loaded.
推荐答案
您可以尝试限制紧急加载
You could try constraining the eager loading
$data = Accommodation::with('accommodationFacilities', 'city')
->take(10)
->with(['accommodationRooms.roomPricingHistory' => function($query){
$query->limit(2)
}])
->limit(2)
->where('is_deleted', 0)
->paginate(10);
文档: https://laravel.com/docs/5.8/eloquent-relationships#eager-loading
这篇关于限制Laravel中嵌套关系的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!