How to select latest rows from approver table

I want to select latest row from approver table based on a request id.
Also i need to join request table and approver table based on request id

Tried latest() method but it will return only last record. But i want to retrieve last last row of each request id.

Tried group by and distinct but not worked. Could you please suggest a better way


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

maybe this would be work, use orderBy(‘column_name’,desc)->first()

Method 2

there are two way to do this :

  1. code a loop and get rows by a query for each row.
  2. try this code :

    DB::table(‘my_table’)->selectRaw(‘*,max(id) as last’)->groupBy(‘user_id’)->get();

Method 3

Use this query:

$last_approver = Approver::orderBy('id', 'DESC')->first();

Method 4

    select * from approver where request_id = <your request id> order by <some column like created_at> limit 1

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x