【发布时间】:2021-05-11 10:13:04
【问题描述】:
我需要在 laravel 的 eloquent 中使用子查询从一个表中选择所有列和一个附加列。例如,
SELECT *, (SELECT COUNT(*) FROM transactions WHERE transactions.customer=customers.id) AS transactions FROM customers
现在,我想出了一个使用 selectRaw 的原始查询,例如:
$customers = Customer::selectRaw('*, (SELECT COUNT(*) FROM transactions WHERE transactions.customer=customers.id) AS transactions')->get();
但我想以雄辩的方式进行,而不使用原始查询。
【问题讨论】: