将SQL转换为Laravel Eloquent语句

时间:2018-04-27 15:47:23

标签: laravel eloquent

我一直在研究几个表格,通过一个相当复杂的关系(我正在尝试清理,但我仍然需要通过我的Laravel从数据中做出报告)。

目前,我可以使用以下SQL查询将数据提取到我的MySQL数据库:

SELECT
customers.id,
customers.customer_name,
SUM(shipments.balance) AS shipmentBalance

FROM customers

LEFT JOIN shipments 
ON customers.id = shipments.bill_to  
AND balance > (SELECT IFNULL(SUM(payments_distributions.amount),0)                
    FROM payments_distributions               
    WHERE payments_distributions.shipment_id = pro_number)
GROUP BY customers.id, customers.customer_name
ORDER BY shipmentBalance DESC
LIMIT 5;

我只是不确定如何将它正确地重写到Laravel Eloquent所需的whereRaw或DB :: raw语句中,因为我以前的尝试都失败了。

更新

这是我尝试过的最接近的解决方案:

DB::table('customers')
        ->select('customers', DB::raw('SUM(shipments.balance) AS shipmentBalance'))
        ->leftJoin(
                 DB::raw('
                        (select shipments 
                        ON customers.id = shipments.bill_to
                        AND balance > (SELECT IFNULL(SUM(payments_distributions.amount),0) 
                            FROM payments_distributions 
                            WHERE payments_distributions.shipment_id = pro_number)'))
        ->groupBy('customers.id')
        ->orderByRaw('shipmentBalance DESC')
        ->limit(5)
        ->get();

更新2

编辑Dom:

根据您的回答使用所有内容,我得到以下回复:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'on clause' (SQL: select customers.id, customers.customer_name,SUM(s.balance) AS shipmentBalance from `customers` left join `shipments` as `s` on `customers`.`id` = `s`.`bill_to` and s.balance > (SELECT IFNULL(SUM(payments_distributions.amount),0) FROM payments_distributions WHERE payments_distributions.shipment_id = s.pro_number) = `` group by `customers`.`id`, `customers`.`customer_name` order by SUM(s.balance) DESC limit 5)

但是如果我删除这一部分,它会显示页面和客户(虽然顺序错误,因为我删除了一个必要的组件:

$join->on(DB::raw('s.balance > 
            (SELECT IFNULL(SUM(payments_distributions.amount),0)                
            FROM payments_distributions               
            WHERE payments_distributions.shipment_id = s.pro_number)
                                            ')); 

有什么我可以提供给你的具体陈述来处理你的整个答案吗?

2 个答案:

答案 0 :(得分:1)

如果没有包含关系的模型或能够在此特定项目上进行测试,这是我能够考虑执行任务的最有说服力的方式。

从客户模型开始的好处是,您将拥有laravel collection,并且可以根据需要paginate。另请查看eloquent docs,它们可以帮助您了解所有不同的选项。希望他的帮助。

P.S。首先在控制器中使用您的模型,或者使用以下任何位置放置此查询:

sys/dirent.h

查询

use App\Customer

答案 1 :(得分:1)

使用此:

DB::table('customers')
    ->select('customers.id', 'customers.customer_name', DB::raw('SUM(shipments.balance) AS shipmentBalance'))
    ->leftJoin('shipments', function($join) {
        $join->on('customers.id', 'shipments.bill_to')
            ->where('balance', '>', function($query) {
                $query->selectRaw('IFNULL(SUM(payments_distributions.amount),0)')
                    ->from('payments_distributions')
                    ->where('payments_distributions.shipment_id', DB::raw('pro_number'));
            });
    })
    ->groupBy('customers.id', 'customers.customer_name')
    ->orderByDesc('shipmentBalance')
    ->limit(5)
    ->get();