如何在Laravel中减少SQL查询

时间:2015-03-31 05:36:31

标签: php sql laravel-5

我正在开发一个项目,在那里我使用了预先加载,我希望减少SQL查询

ScreenShot1

enter image description here

ScreenShot2

enter image description here

public function showAllOrdersbySite($domain_id){
    $orders = Order::whereHas('paymentStatus', function ($query) use($domain_id) {
        $query->where("domain_id","=",$domain_id);

    })->orderby('tbl_orders.created_at', 'desc')->paginate(20);
        return View('admin.all_orders')
            ->with('orders',$orders);
}

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我不知道要添加功能,我只是通过添加这样的方法来解决我的问题 来源:Eager Load Constraints Filter issue in Laravel

public function showAllOrdersbySite($domain_id){
    $orders = Order::with('func1',func2)->whereHas('paymentStatus', function ($query) use($domain_id) {
        $query->where("domain_id","=",$domain_id);

    })->orderby('tbl_orders.created_at', 'desc')->paginate(20);
        return View('admin.all_orders')
            ->with('orders',$orders);
}

答案 2 :(得分:0)

使用Laravel Eager加载或Laravel LAzy Eager Loading。 Laravel LAzy渴望加载满足您的要求