分页并将搜索过滤器应用于laravel中的Raw查询

时间:2019-06-13 09:55:14

标签: php laravel

我在laravel中有一个原始查询,我想将其转换为laravel查询,以便对该查询应用搜索过滤器和分页。

$transactions = DB::select("
    SELECT transaction1.* FROM ws_transactions transaction1  
    INNER JOIN ws_payment_requests PR  
    ON transaction1.fk_id = PR.id  
    where transaction1.fk_type = 'amount_requesteddd' AND PR.user_id = " . $user->id . "  
UNION  
    SELECT transaction2.*  
    FROM ws_transactions transaction2  
    INNER JOIN ws_deal_orders DO ON transaction2.fk_id = DO.id  
    INNER JOIN ws_deals ON ws_deals.id = DO.deal_id  
    where transaction2.fk_type = 'deal_purchasedd' AND ws_deals.user_id = " . $user->id . "  
UNION  
    SELECT transaction3.* FROM ws_transactions transaction3  
    INNER JOIN ws_payments PAY ON transaction3.fk_id = PAY.id  
    INNER JOIN users ON users.id = PAY.to_user_id  
    where transaction3.fk_type = 'amount_released' AND users.id = " . $user->id . "  
UNION  
    SELECT transaction4.* FROM ws_transactions transaction4  
    INNER JOIN ws_voucher_codes VC ON transaction4.fk_id = VC.id  
    INNER JOIN ws_deal_orders ON ws_deal_orders.id = VC.order_id and VC.class = -1  
    INNER JOIN ws_deals ON ws_deal_orders.deal_id = ws_deals.id  
    where transaction4.fk_type = 'deal_redeemed' AND ws_deals.user_id = " . $user->id . " AND VC.status = 1  
UNION  
    SELECT transaction4.* FROM ws_transactions transaction4  
    INNER JOIN ws_voucher_codes VC ON transaction4.fk_id = VC.id  
    INNER JOIN ws_deals ON ws_deals.id = VC.class and VC.class != -1  
    where transaction4.fk_type = 'deal_redeemed' AND ws_deals.user_id = " . $user->id . " AND VC.status = 1  
    ORDER BY created_at DESC "  
);

1 个答案:

答案 0 :(得分:0)

您可以使用内置分页方法查询结果。

$transactions->pagination(10);

您可以决定每页显示多少个