dongxia19772008 2015-02-25 05:54
浏览 48
已采纳

Laravel Eager with Where约束

I have 2 methods with eager loading and paginate. but problem is that i have to use where constraint too and i am getting offset 0 error.

public function showAllOrders(){
    $orders = \admin\Order::with('orderfiles','paymentStatus','orderStatus','orderDelivery','flexibleDelivery')->paginate(20);
        return View('admin.all_orders')
            ->with('orders',$orders);
}

public function showAllPaidOrders(){
    $orders = \admin\Order::with(array('orderfiles','orderStatus','orderDelivery','flexibleDelivery','paymentStatus'=>function($query){
        $query->where('status', '=', 'Paid');
    }))->paginate(20);
        return View('admin.all_orders')
            ->with('orders',$orders);
}

showAllOrders working fine but when i am using where constraint with showAllPaidOrders i am getting offset error....

1/2 ErrorException in Collection.php line 837: Undefined offset: 0 2/2 ErrorException in Collection.php line 837: Undefined offset: 0 (View: E:\content\xampp\htdocs\l5esources\views\admin\all_orders.blade.php)

Even if both blade views are same. The only difference is the "Where Constraint"

  • 写回答

2条回答 默认 最新

  • dph23577 2015-03-31 05:37
    关注

    i solved this with using wherehas function

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化