Laravel 使用Elount更新选择计数
我有一个雄辩的查询,返回表中10个最常见的名称Laravel 使用Elount更新选择计数,laravel,eloquent,laravel-5.2,Laravel,Eloquent,Laravel 5.2,我有一个雄辩的查询,返回表中10个最常见的名称 $topPeople = $model ->select(['name', DB::raw('COUNT(*) as count')]) ->groupBy('name') ->orderBy('count', 'desc') ->take(10) ->get(); 这可以很好地返回结果,但我想更新这些记录,而不是返回数据。当我使用update而不是get时 $model
$topPeople = $model
->select(['name', DB::raw('COUNT(*) as count')])
->groupBy('name')
->orderBy('count', 'desc')
->take(10)
->get();
这可以很好地返回结果,但我想更新这些记录,而不是返回数据。当我使用update而不是get时
$model
->select(['name', DB::raw('COUNT(*) as count')])
->groupBy('name')
->orderBy('count', 'desc')
->take(10)
->update(['popular' => 1]);
我得到了错误
SQLSTATE[42S22]:未找到列:“order子句”中的1054未知列“count”
更新这些记录中的
流行字段的有效方法是什么?选择ID,然后使用这些ID执行更新。下面是一个使用您的代码的示例,略有更改:
$topPeople = SomeModel
->select(['id', DB::raw('COUNT(*) as count')])
->groupBy('id')
->orderBy('count', 'desc')
->take(10)
->pluck('id');
SomeModel::whereIn('id', $topPeople)->update(['popular' => 1]);
选择ID,然后使用这些ID执行更新。下面是一个使用您的代码的示例,略有更改:
$topPeople = SomeModel
->select(['id', DB::raw('COUNT(*) as count')])
->groupBy('id')
->orderBy('count', 'desc')
->take(10)
->pluck('id');
SomeModel::whereIn('id', $topPeople)->update(['popular' => 1]);
拔这就是我想要的功能,谢谢,很高兴它能帮上忙。:)拔这就是我想要的功能,谢谢,很高兴它能帮上忙。:)