将Laravel原始查询作为条件集合

时间:2019-07-15 22:01:41

标签: laravel laravel-5

我有一个相当复杂的查询,我不想用Eloquent编写,所以我将其编写为原始文件。唯一的问题是,我需要能够使用此查询所连接的前端来搜索/过滤数据。

这是我尝试过的,但是却收到“在null上调用成员函数get()的错误”。

这是我的代码:

$report = collect(DB::connection('mysql2')->select("SELECT
            t2.holder,
            t2.merchantTransactionId,
            t2.bin,
            t2.last4Digits,
            t3.expDate,
            (CASE WHEN t3.expDate < CURDATE() THEN 'Expired'
            WHEN t3.expDate > CURDATE() THEN 'Due to expire' END) AS expInfo,
            t2.uuid
            FROM transactions AS t2
            INNER JOIN (
                SELECT t1.uuid, t1.holder, t1.bin, t1.last4Digits, LAST_DAY(CONCAT(t1.expiryYear, t1.expiryMonth, '01')) AS expDate
                FROM transactions t1
                JOIN (SELECT t1.merchant_access
                        FROM total_control.users, 
                        JSON_TABLE(merchant_access, '$[*]' COLUMNS (
                            merchant_access VARCHAR(32) PATH '$')
                        ) t1
                        WHERE users.id = :userId
                        ) AS t2
                    ON t1.merchantUuid = t2.merchant_access
                WHERE t1.paymentType = 'RG'
                AND t1.status = 1
            ) t3
            ON t2.uuid = t3.uuid
            WHERE t3.expDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)
            GROUP BY t2.holder, t2.bin, t2.last4Digits
            ORDER BY t2.holder ASC", ['userId' => $request->userId]))
            ->when($request->search['holder'], function($q) use ($request) {
                $q->where('t2.holder', 'LIKE', '%'.$request->search['holder'].'%');
            })->get();

return $report;

0 个答案:

没有答案