Anna
Anna

Reputation: 13

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

Upvotes: 1

Views: 60

Answers (4)

Mohammad Akbari
Mohammad Akbari

Reputation: 446

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

Upvotes: 0

Masoud Hosseini
Masoud Hosseini

Reputation: 46

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

Upvotes: 0

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2844

Use this query:

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

Upvotes: 0

MadushanWijesuriya
MadushanWijesuriya

Reputation: 11

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

Upvotes: 1

Related Questions