假设用户中心有一行表格为粉丝/支持者数量列,但是这行并不存在于数据库中。数据是由 $user->followers->count()$user->followings->count() 获取。通过 withCount 方法即可解决:

    1. Grid::make(new Model(),function(Grid $grid) {
    2. $grid->model()->withCount('followers','followings');
    3. $grid->column($type."_count",'粉丝/支持者数量')->sortable();
    4. })

    $type 为外部传入的变量 : followers 或 followings