Reputation: 13
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
Reputation: 446
select * from approver where request_id = <your request id> order by <some column like created_at> limit 1
Upvotes: 0
Reputation: 46
there are two way to do this :
code a loop and get rows by a query for each row.
try this code :
\DB::table('my_table')->selectRaw('*,max(id) as last')->groupBy('user_id')->get();
Upvotes: 0
Reputation: 2844
Use this query:
$last_approver = Approver::orderBy('id', 'DESC')->first();
Upvotes: 0
Reputation: 11
maybe this would be work, use orderBy('column_name',desc)->first()
Upvotes: 1