我想显示前5位热门用户,其中包含大多数帖子:User1(5帖子)User5(3张帖子)User2(2张帖子).... -
在我的AppServiceProvider.php
view()->composer('questions.side', function($view){
$users = User::leftJoin('questions','user.id','=','questions.user_id')
->selectRaw('user.*, count(questions.user_id) AS total')
->groupBy('users.id')
->orderBy('total','DESC')
->skip(0)
->take(5)
->get();
$view->with('users', $users);
});
在我看来:
foreach($usersas $user) {
var_dump($user);
}
但是我遇到了这个问题:
AppServiceProvider.php第18行中的FatalErrorException:语法错误, 意想不到的'' (T_STRING)
答案 0 :(得分:0)
错误是因为sql语法错误 从
更改您的$ users$users = User::leftJoin('questions','user.id','=','questions.user_id')->selectRaw('user.*, count(questions.user_id) AS total')->groupBy('users.id')->orderBy('total','DESC')->skip(0)->take(5)->get();
到这个
$users = DB::table('users')->leftJoin('questions','user.id','=','questions.user_id')->select('user.*', count('questions.user_id') 'AS total')->orderBy('total','DESC')->skip(0)->take(5)->get();
在你在刀片文件中解析它们之前,我建议你使用var_dump($ users)