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

设置商品规格


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

php
// 更新商品规格
public function updateSkus(){
	$params = request()->param();
	$goods = request()->Model;
	$GoodsSkus = new \app\model\GoodsSkus();
	// 单规格
	if ($params['sku_type'] == 0) {
		// 原本多规格
		if ($goods->sku_type == 1) {
	        $GoodsSkus->where('goods_id',$goods->id)->delete();
		}
		$goods->sku_type = 0;
		$goods->sku_value = $params['sku_value'];
		$res = $goods->save();
		return showSuccess($res);
	}
	// 多规格
	$goods->sku_type = 1;
	$goods->save();
	// 清除多规格
       $GoodsSkus->where('goods_id',$goods->id)->delete();
	// 创建新的
	$res = $GoodsSkus->saveAll($params['goodsSkus']);
	return showSuccess($res);
}

验证器 app/validate/admin/Goods.php

php
// 验证规则
protected $rule = [
    ...
    'id'=>'require|integer|>:0|isExist:Goods',
	'sku_type'	=> 'require|in:0,1',		
    'sku_value'=> 'requireIf:sku_type,0|array',	
	'goodsSkus'=>'requireIf:sku_type,1|array',
    ...
];

protected $scene = [
    // ...
    'updateSkus'=>['id','sku_type','sku_value','goodsSkus']
];

路由 router/admin.php

php
Route::post('goods/updateskus/:id','admin.Goods/updateSkus');