2016-09-17 32 views
0

我想顯示前5個最熱門的用戶,大多數帖子是這樣的:User1(5帖子)User5(3帖子)User2(2帖子).... - 在我的AppServiceProvider.php顯示laravel中大多數帖子的用戶5.2

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); 
} 

,但我得到這個問題:

FatalErrorException in AppServiceProvider.php line 18: syntax error, unexpected '‌​‌​‌​‌​' (T_STRING)

回答

0

錯誤是因爲SQL語法錯誤的 你的$用戶

$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($用戶),解析片文件中之前他們

+0

它給了我一些錯誤:語法錯誤,意外''(T_STRING) 我試試這個:view() - >作曲家('questions.side',function($ view){$ users = User :: leftJoin('questions','users.id','=','questions.user_id') - > select原始('us ers。*,count(questions.user_id)AS total') - > groupBy('users.id') - > orderBy('total','DESC') - > skip( 0) - >取(5) - >得到(); \t \t \t $ view-> with('users',$ users); }); Not working to .. –

+0

您是否嘗試將代碼更改爲我爲您鍵入的內容?你的代碼有語法錯誤,它不會像這樣工作 – Nour

+0

我修改了代碼,我認爲不需要分組,因爲你想爲每個用戶分配一行 – Nour