On this page
增加商品类型
控制器 app/controller/admin/GoodsType.php
php
public function save(Request $request)
{
$res = $this->M->save($request->param());
return showSuccess($res);
}
模型 app/model/GoodsType.php
php
// 增加后
public static function onAfterInsert($goodsType){
// 关联创建规格类型值
$valueList = request()->param('value_list');
$count = is_array($valueList) ? count($valueList) : 0;
if ($count > 0) {
$GoodsTypeValue = new GoodsTypeValue();
for ($i = 0; $i < $count; $i++) {
$valueList[$i]['goods_type_id'] = $goodsType->id;
}
$GoodsTypeValue->saveAll($valueList);
}
// 关联对应商品规格
$skusId = request()->param('skus_id');
// 判断是否为空
if (empty($skusId) || !is_array($skusId)) return;
// 获取当前类型所有属性
$d = Skus::where('id','in',$skusId)->field('id')->select()->toArray();
$d = array_map(function($item) use($goodsType){;
return [
'skus_id'=>$item['id'],
'goods_type_id'=>$goodsType->id
];
},$d);
(new SkusGoodsType)->saveAll($d);
}
验证器 app/validate/admin/GoodsType.php
php
// 验证规则
protected $rule = [
'status'=>'require|in:0,1',
'name'=>'require',
'order'=>'integer|>=:0',
'skus_id'=>'array',
'value_list'=>'array',
];
protected $scene = [
...
'save'=>['name','status','order','skus_id','value_list'],
];
路由 router/admin.php
php
Route::post('goods_type','admin.GoodsType/save');