0
点赞
收藏
分享

微信扫一扫

laravel5.6 eloquent with 通过闭包筛选特定 field


一. 一对一关系
hasOne
通过外键关联

goods_order表和goods_order_status表

goods_orde模型

laravel5.6 eloquent with 通过闭包筛选特定 field_外键关联


使用with查询某个订单号及其的状态

GoodsOrder::where($where)
                    ->select('uid','userinfo_id','platform_type','order_id','account_uid','order_time','goods_id','goods_name','pict_url','pay_money','num','commission_rate','commission','rights_cash','calculate_time')
                    ->orderBy('order_time','desc')
                    ->with(['orderId' => function ($query){
                        $query->select(['status','order_id']);  //order_id 是外键,不加不显示关联的数据
                    }])
                    ->paginate($parameter['limit'])
                    ->toArray();

使用加where条件

$status = self::WAIT;
                $where = [
                    'platform_type' => $parameter['platform_type'],
                    'uid' => $uid,
                ];
                $goodsOrder = GoodsOrder::where($where)
                        ->select('uid','userinfo_id','platform_type','order_id','account_uid','order_time','goods_id','goods_name','pict_url','pay_money','num','commission_rate','commission','rights_cash','calculate_time')
                        ->orderBy('order_time','desc')
                        ->with(['orderId' => function ($query) use($status){
                            $query->where(['status'=>$status])->select(['status','order_id']);
                        }])
                    ->paginate($parameter['limit'])
                    ->toArray();
                $goodsOrder = $this->emptyErr->nullToEmpty($goodsOrder);
                return $goodsOrder;


举报

相关推荐

0 条评论