find()->toArray(); if(!$goods_info['depot']) return true; $goods_item = BaseGoodsItem::field('id,goods_spec')->where(['goods_id'=>$goods_id])->select()->toArray(); $depot_arr = explode(',',$goods_info['depot']); foreach ($depot_arr as $v) { $depot_goods = DepotGoods::where(['depot_id'=>$v,'goods_id'=>$goods_id])->find(); if($depot_goods) continue; // 商品加入仓库 $depot_info = DepotGoods::create(['depot_id'=>$v,'goods_id'=>$goods_id]); // 规格添加 $item = []; foreach ($goods_item as $iv) { $item[]= [ 'depot_id'=>$v, 'goods_id'=>$depot_info->id, 'spec_id'=>$iv['id'], 'goods_spec'=>$iv['goods_spec'], 'base_goods'=>$goods_id, ]; } (new DepotGoodsItem())->insertAll($item); } return true; } public static function getBaseGoods() { $goods_list = self::with('itemList')->where(['is_deleted'=>0])->order('sort desc ,id desc')->column('id,name,depot','id'); return $goods_list; /*$goods_list = self::where(['is_deleted'=>0])->order('sort desc ,id desc')->column('id,name,depot','id'); array_walk($goods_list,function(&$v,$k){ $v['list'] = BaseGoodsItem::field('id,goods_spec,goods_no,stock,original_price,sell_price') ->where(['goods_id'=>$v['id']])->order('i')->select()->toArray(); });*/ return $goods_list; } public function itemList() { return $this->hasMany('BaseGoodsItem','goods_id','id')->field('id,goods_spec,goods_no,stock,original_price,sell_price')->where(['is_deleted'=>0,'status'=>1]); } }