我有 2 个型号。帖子模型和类别模型。
class PostModel extends Model
{
protected $table='posts';
protected $primaryKey = 'id';
protected $guarded=['id'];
public function categories()
{
return $this->belongsTo(CategoriesModel::class);
}
}
class CategoriesModelextends Model
{
protected $table='categories';
protected $primaryKey = 'id';
protected $guarded=['id'];
public function posts()
{
return $this->hasMany(PostModel::class);
}
}
我想获得 6 个类别和 10 个帖子。我在我的控制器中使用了这段代码
$categories = CategoriesModel::with(['pages' => function($query) {
$query->limit('10');
}])->take("6")->get();
但这段代码是错误的。它适用于所有记录。但事实是此查询适用于每个类别。请帮助我,谢谢
暮色呼如
忽然笑
梦里花落0921
DIEA