假设用户中心有一行表格为粉丝/支持者数量列,但是这行并不存在于数据库中。数据是由 $user->followers->count()
或$user->followings->count()
获取。通过 withCount
方法即可解决:
Grid::make(new Model(),function(Grid $grid) {
$grid->model()->withCount('followers','followings');
$grid->column($type."_count",'粉丝/支持者数量')->sortable();
})
$type
为外部传入的变量 : followers 或 followings