mercredi 1 février 2017

Laravel multiple withCount on the same relationship

I need to count the results from two different conditions, from the same relationship, but it's being returned as the same name.

Model::where('types_id', $specialism_id)
        ->withCount(['requests' => function ($query) {
            $query->where('type', 1);
        }])
        ->withCount(['requests' => function ($query) {
            $query->where('type', 2);
        }])

I can access the withCount using $model->requests_count, but because it's querying the same relationship, it appears to overwrite it:

select count(*) 
    from `requests` where `requests`.`id` = `model`.`id` 
    and `type` = '1') as `requests_count`, 
(select count(*) from `requests` where `requests`.`id` = `model`.`id` 
    and `type` = '2') as `requests_count`

How can I specify the name for multiple withCount?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kWgFDv
via IFTTT

Aucun commentaire:

Enregistrer un commentaire