Skip to content
关注公众号,获取新课通知

发布商品


表单数据部分:

控制器 app/controller/admin/Goods.php

php
protected $excludeValidateCheck = ['create'];
public function create()
    {
    	// 分类
        $cates = (new \app\model\Category())->select();
        $cates = list_to_tree($cates->toArray(),'category_id');
        // 运费模板
        $express = (new \app\model\Express())->select();
        // 商品类型列表
        $type = (new \app\model\GoodsType())->with(['goodsTypeValues'=>function($q){
        	$q->where('status',1);
        }])->where('status',1)->select();
        // 获取goods_id为0的商品规格列表
        $goodsSkusCard = (new \app\model\GoodsSkusCard())->with(['goodsSkusCardValue'])->where('goods_id',0)->select();
        return showSuccess(compact('cates','express','type','goodsSkusCard'));
    }

模型 app/model/GoodsType.php

php
// 关联规格值
   public function goodsTypeValues(){
       return $this->hasMany('GoodsTypeValue');
   }

模型 app/model/GoodsSkusCard.php

php
// 关联对应的值
   public function goodsSkusCardValue(){
       return $this->hasMany('GoodsSkusCardValue')->order([ 'order'=>'ASC' ]);
   }

路由 router/admin.php

php
Route::get('goods/create','admin.goods/create');

提交保存部分:

控制器 app/controller/admin/Goods.php

php
public function save(Request $request)
{
    $res = $this->M->save($request->param());
    return showSuccess($res);
}

验证器 app/validate/Goods.php

php
// 验证规则
protected $rule = [
    'status'=>'require|in:0,1',
        'title'	=>'require|NotEmpty',	 		
        'category_id' =>'require|integer|>=:0|isExist:Category,false',
        'cover'	=>'url',	       
        'unit' => 'require|NotEmpty',	        
        'stock'  => 'require|integer|>=:0',	   
        'min_oprice'  => 'require|float|>=:0',	 
        'min_stock'=>'require|integer|>=:0',	
        'ischeck'=>'require|in:0,1,2',	 	     	
        'stock_display'	=> 'require|in:0,1',	
        'express_id'=> 'require|integer|>:0|isExist:Express,false',
        'sku_type'	=> 'require|in:0,1',		
        'sku_value'=> 'requireIf:sku_type,0|array',	
        'goods_type_id'	=> 'require|integer|>:0|isExist:GoodsType,false',
        'content'=> 'require',			
        'discount'=> 'require|integer|between:0,100', 	
        'order'	=> 'require|integer|>:0',	
        'goods_skus_card_ids'=>'requireIf:sku_type,1|array|NotEmpty',
        'goods_attrs'=>'require|array|NotEmpty',
        
        'goods_skus'=>'requireIf:sku_type,1|array',
];

protected $scene = [
    // ...
    'save'=>["title","category_id","cover","desc", "unit","stock","min_stock","ischeck","status","stock_display","express_id","min_oprice"],
];

路由 router/admin.php

php
Route::post('goods','admin.Goods/save');