B
B
BonBon Slick2017-05-26 14:26:09
PostgreSQL
BonBon Slick, 2017-05-26 14:26:09

Select last unique record via Eloquent Laravel?

$messages = $user->messagesReceived()->groupby('to_user_id')->distinct()->orderBy('created_at', 'DESC')->get();
// и
$messages = $user->messagesReceived()->groupby('to_user_id')->orderBy('created_at', 'DESC')->get();

Gives errors:
Illuminate \ Database \ QueryException (42803)
SQLSTATE[42803]: Grouping error: 7 ERROR: column "messages.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: select distinct * from "messages" where "messages"."to_user_... ^ (SQL: select distinct * from "messages" where "messages"."to_user_id" = 1 and "messages"."to_user_id" is not null group by "to_user_id" order by ....

I'm trying to make a selection of all messages, but group them by user ID, sorting them by the date they were sent. Thus, the most recent message will be displayed, then I load the rest by clicking on the message, something in the spirit of VK dialogs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pantagruel964, 2017-05-26
@BonBonSlick

You don't read the documentation at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question