Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按相关表计数的Laravel顺序_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 按相关表计数的Laravel顺序

Php 按相关表计数的Laravel顺序,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有一个模型餐厅,一个餐厅有一个或多个订单。我怎样才能找到订单最多的顶级餐厅?比如说前五名 我不太确定如何用一个雄辩的模型来实现这一点 编辑: 通过原始查询,我可以找到要构建的正确查询 SELECT R.name, count(O.id) as total FROM restaurants AS R LEFT JOIN orders as O ON R.id = O.restaurant_id GROUP BY R.id ORDER BY total DESC 但我不知道如何用Flu

我有一个模型
餐厅
,一个
餐厅
有一个或多个
订单
。我怎样才能找到订单最多的顶级餐厅?比如说前五名

我不太确定如何用一个雄辩的模型来实现这一点

编辑:

通过原始查询,我可以找到要构建的正确查询

SELECT 
    R.name, count(O.id) as total
FROM restaurants AS R
LEFT JOIN orders as O ON R.id = O.restaurant_id
GROUP BY R.id
ORDER BY total DESC
但我不知道如何用
Fluent

看起来我找到了我要找的东西:

return Restaurant::select(['*', DB::raw('count(orders.id) as total')])
->leftJoin('orders', 'restaurants.id', '=', 'orders.restaurant_id')
->groupBy('restaurants.id')
->orderBy('total', 'DESC')
->limit(5);

我已将答案添加到主题开始

你试过什么?您的
型号是什么样子的?我们需要看一些代码来帮助您。