Home > Software engineering >  How can I make this query using eloquent?
How can I make this query using eloquent?

Time:06-21

I need to convert this statement using eloquent but don't know how... I've tried some different methods but none of them worked.

SELECT c.id, c.sap_id, 
(SELECT max(last_buy) from clients_summary_data csd 
WHERE csd.client_id = c.id) as last_buy 
from clients c
where c.deleted_at is NULL 

CodePudding user response:

Consider your model name of clients is "Client" and your relation name is "clients_summary"

Client::query()
    ->withMax('clients_summary', 'last_buy')
    ->get(['id','sap_id']);
  • Related