0

I have the following query :

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

The count isn't working, Its supposed to be working !

The following error comes up :

Column not found: 1054 Unknown column 'COUNT(gift_sents.receiver_id' in 'field list' (SQL: selectusers.*,COUNT(gift_sents.receiver_idastotal_postsfromusersinner joingift_sentsongift_sents.receiver_id=users.idgroup bygift_sents.idorder bytotal_postsasc limit 3)

3
  • You have a typo in your select statement, make sure to close your COUNT open parenthesis. ->select('users.*', 'COUNT(gift_sents.receiver_id as total_posts)') Commented Jul 16, 2016 at 18:53
  • Still not working ! Commented Jul 16, 2016 at 18:55
  • 1
    Error: Unknown column 'count(gift_sents.receiver_id)' Commented Jul 16, 2016 at 18:56

2 Answers 2

6

i think it should be:

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

see doc here - 'Raw Expressions' section

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of:

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

you should use:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.