2016-07-16 4 views
0
私は次のクエリを持って

でカウントする方法:Laravelは:クエリ

カウントが動作していない
App\User::join('gift_sents', function($builder){ 
     $builder->on('gift_sents.receiver_id', '=', 'users.id'); 
     }) 
     ->select('users.*', 'COUNT(gift_sents.receiver_id as total_posts') 
     ->groupBy('gift_sents.id') 
     ->orderBy('total_posts', 'ASC') 
     ->limit(3)->get(); 

、その作業をすることになって!

次のエラーがアップします:.*, COUNT(gift_sents .as total_posts fromユーザーon gift_sents .=ユーザー. ID group by gift_sents . ID order by total_posts asc limit 3)

+0

あなたが使用する必要があります
->select('users.*', 'COUNT(gift_sents.receiver_id as total_posts') 

を開いている括弧のCOUNTを閉じます。 ' - > select( 'users。*'、 'C​​OUNT(gift_sents.receiver_id as total_posts)')' –

+0

まだ動作しません! – Gammer

+1

エラー:「不明な列の数(gift_sents.receiver_id)」「 – Gammer

答えて

0

の代わりに:に確認してください、あなたはあなたのselect文にタイプミスがあり

->selectRaw('users.*, COUNT(gift_sents.receiver_id) as total_posts') 
3

私はそれを考えるとreceiver_id inner join gift_sentsをreceiver_id

Column not found: 1054 Unknown column 'COUNT(gift_sents.receiver_id' in 'field list' (SQL: selectユーザー次のとおりです。

->select('users.*', DB::raw('COUNT(gift_sents.receiver_id) as total_posts')) 

ドキュメントhere見る - '生の表現' セクション

+0

あなたは正しいです!ちょっとした編集が必要です! – Gammer