mardi 11 mai 2021

Laravel Eloquent - Select all columns plus a subquery using Eloquent only

I need to select all columns from a table and an additional columns using subquery. For example,

SELECT *, (SELECT COUNT(*) FROM transactions WHERE transactions.customer=customers.id) AS transactions FROM customers

Right now, I have come up with using selectRaw like

$customers = Customer::selectRaw('*, (SELECT COUNT(*) FROM transactions WHERE transactions.customer=customers.id) AS transactions')->get();

But I'd like to do it the eloquent way without using raw queries.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire