dqk42179 2019-02-02 17:35
浏览 158

Laravel Eloquent COUNT(*)没有结果GroupBy()

I have query on laravel

$types=Types::selectRaw('COUNT(property_types.id) as total, property_types.types, property_types.id')

               ->join('properties', 'property_types.id', '=', 
                      'properties.property_type')
               ->where('property_purpose', '"Sale"')
               ->groupBy('property_types.id','property_types.id','property_types.types')
               ->get();

And I dont have any result

Collection {#383 ▼

#items: []

I change ->get() by ->toSql(); And I have laravel query

select COUNT(property_types.id) as total, property_types.types, property_types.id 
from `property_types` 
inner join `properties` on `property_types`.`id` = `properties`.`property_type`
where `property_purpose` = ? 
group by `property_types`.`id`, `property_types`.`id`, `property_types`.`types`

Change ? by variable "Sale" copy and paste on PhpMyadmin and I got te result that I want

enter image description here

I dont know what wrong on Laravel Eloquent!

  • 写回答

1条回答 默认 最新

  • dongshangan2074 2019-02-02 19:10
    关注

    That work for me

    $types=Types::selectRaw('COUNT(property_types.id) as total, property_types.types, property_types.id')

               ->join('properties', 'property_types.id', '=', 
                      'properties.property_type')
               ->where('property_purpose', 'Sale')
               ->groupBy('property_types.id','property_types.id','property_types.types')
               ->get();
    
    评论

报告相同问题?