Saturday, May 5, 2018

Laravel – select rows with first occurance of column value - development

Laravel – select rows with first occurance of column value

I have DB table similar to this

id | user_id | name
----------------------------
1  | 56      | John
2  | 25      | Carry
3  | 56      | Jim
4  | 12      | Jolana
5  | 12      | Ava 

What I need to do is to select the rows with the first occurance of user_id ordered in DESC by id. In this case it would mean to select rows with IDs 5, 3 and 2.

Can this be achieved somehow using Eloquent, or if not, pure SQL? I can imagine writing a script for this but I would like to make it work with one query.



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-select-rows-with-first-occurance-of-column-value/
via Lzo Media

No comments:

Post a Comment