在laravel 5.2中显示大多数帖子的用户

时间:2016-09-17 01:18:58

标签: laravel laravel-5 laravel-5.2

我想显示前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('‌​us‌​er‌​.*, 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)

1 个答案:

答案 0 :(得分:0)

错误是因为sql语法错误 从

更改您的$ users
$users = User::leftJoin('questions','user.id','=','questions.user_id'‌​‌​‌​‌​)->selectRaw('‌​us‌​er‌​.*, 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('‌​us‌​er‌​.*', count('questions.user_id') 'AS total')->orderBy('total','DESC')->skip(‌​‌​‌​0)->take(5)->get‌​()‌​;

在你在刀片文件中解析它们之前,我建议你使用var_dump($ users)