Laravel 雄辩-如何在关系的子查询中使用父列?
我有两种型号:Laravel 雄辩-如何在关系的子查询中使用父列?,laravel,eloquent,laravel-7,Laravel,Eloquent,Laravel 7,我有两种型号: 类别带有id,随机, Post带有id,cat\u id,… 两个模型中的关系正常 类别模型中的随机字段为整数 0:不随机获取Post的所有记录 1,2,3,…:将按随机顺序和限制(随机)的Post 现在,我想编写一个one查询,用random列获取Category和Post $cat_id = 1; $cat = Category::with(['posts'=>function($q){ //I want to get categories table here
类别
带有id,随机,
Post
带有id,cat\u id,…
两个模型中的关系正常
类别
模型中的随机
字段为整数
:不随机获取0
的所有记录Post
:将1,2,3,…
按随机顺序
和
的限制(随机)
Post
random
列获取Category
和Post
$cat_id = 1;
$cat = Category::with(['posts'=>function($q){
//I want to get categories table here to compare and limit
//like it
if(cagetory.random > 0){
$q->inRandomOrder()->limit(category.random);
}
}])->where('id',$cat_id)->first();