mtvs_dev
4 years ago

Adding a subquery to an eloquent query

Posted 4 years ago by mtvs_dev

I want to add a field to the result set of an eloquent query that its value is provided by a subquery.

In other words I want the returned eloquent models have an extra field using that subquery.

SELECT * , (
SELECT COUNT( * )
FROM `items` AS `i`
INNER JOIN `places` ON `places`.`item_id` = `i`.`id`
INNER JOIN `participants` ON `participants`.`place_id` = `places`.`id`
WHERE `items`.`id` = `i`.`id`
AND `participants`.`approved` =1
) AS `unapproved_participants`
FROM `items`

so I have both items fields and the unapproved_participants

Please sign in or create an account to participate in this conversation.