0

I have the following SQL query:

return User::where("users.id", $id)
            ->select(array('rating.rate', User::raw('count(offers.id) AS ofrs, count(announcements.id) as appts')))
            ->join('announcements', 'users.id', '=', 'announcements.user_id')
            ->join('offers', 'users.id', '=', 'offers.user_id')
            ->join('rating', 'users.id', '=', 'rating.user_id')
            ->group_by('ofrs')
            ->get();

It gives me an error:

BadMethodCallException in Builder.php line 2508: Call to undefined method Illuminate\Database\Query\Builder::group_by()

Why does not work group by?

Also as way I tried:

  return User::where("users.id", $id)
            ->selectRaw('rating.rate, count(offers.id) AS ofrs, count(announcements.id) as appts')
            ->groupBy('appts')
            ->groupBy('ofrs')
            ->join('announcements', 'users.id', '=', 'announcements.user_id')
            ->join('offers', 'users.id', '=', 'offers.user_id')
            ->join('rating', 'users.id', '=', 'rating.user_id')
            ->get();

Result:

SQLSTATE[42000]: Syntax error or access violation: 1056 Can't group on 'appts'

I have written SQL query directly in SQL:

select rating.rate, COUNT(offers.id) AS ofrs, COUNT(announcements.id) as appts from `users` left join `announcements` on `users`.`id` = `announcements`.`user_id` left join `offers` on `users`.`id` = `offers`.`user_id` left join `rating` on `users`.`id` = `rating`.`user_id` where `users`.`id` = 1

It works, by model is not:

return User::where("users.id", $id)
            ->selectRaw('rating.rate, COUNT(offers.id) AS ofrs, COUNT(announcements.id) as appts')
            ->leftJoin('announcements', 'users.id', '=', 'announcements.user_id')
            ->leftJoin('offers', 'users.id', '=', 'offers.user_id')
            ->leftJoin('rating', 'users.id', '=', 'rating.user_id')
            ->get();

This work:

return User::where("users.id", $id)
            ->selectRaw('rating.rate, COUNT(offers.id) AS ofrs, COUNT(announcements.id) AS appts')
            ->leftJoin('announcements', 'users.id', '=', 'announcements.user_id')
            ->leftJoin('offers', 'users.id', '=', 'offers.user_id')
            ->leftJoin('rating', 'users.id', '=', 'rating.user_id')
            ->groupBy('rating.rate')
            ->get();
7
  • 1
    groupBy('ofrs') not group_by Commented Mar 12, 2017 at 9:59
  • I tried groupBy('ofrs') Commented Mar 12, 2017 at 10:00
  • Do you have the relationships set up in your User model for announcements, offers and rating? Commented Mar 12, 2017 at 10:02
  • Relatively Users? Commented Mar 12, 2017 at 10:03
  • But problem in SQL query, not in models Commented Mar 12, 2017 at 10:05

2 Answers 2

2

The function name you're calling for group by is incorrect. Try:

->groupBy('ofrs')

See here for more.

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

6 Comments

The edit to your question is a completely different problem. In that case, you appear to be trying to group on your counted aggregate. What exactly are you trying to achieve here? This is an entirely different kind of question to your original.
I need to join table User with others tables and to amount rows in related tabled for current user
So you're effectively trying to get the count of offers and announcements for each user?
I have written this Query directly in SQL: select rating.rate, COUNT(offers.id) AS ofrs, COUNT(announcements.id) as appts from users` left join announcements on users.id = announcements.user_id left join offers on users.id = offers.user_id left join rating on users.id = rating.user_id where users.id = 1` It works, but in Laravel does not
Look question please
|
1

You need to read on how SQL works. You can't group by counted alias. What you are looking for is probably three queries or three relations.

You should create relation on User for announcements, second one for offers and third for ratings.

Than you would User::find($id)->announcements->count(), User::find($id)->offers->count() and User::find($id)->ratings->count()

Or you can use your Fluent way with something like:

return User::find($id) ->leftJoin('announcements', 'users.id', '=', 'announcements.user_id') ->count();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.