On this page
设置商品属性
控制器 app/controller/admin/Goods.php
php
// 更新商品属性
public function updateAttrs(){
// 删除之前
$goods_id = request()->Model->id;
request()->Model->goodsAttrs()->where([ 'goods_id'=>$goods_id ])->delete();
// 创建新的
$goods_attrs = request()->param('goods_attrs');
$res = request()->Model->goodsAttrs()->saveAll($goods_attrs);
return showSuccess($res);
}
验证器 app/validate/admin/Goods.php
php
// 验证规则
protected $rule = [
// ...
'id'=>'require|integer|>:0|isExist:Goods',
'goods_attrs'=>'require|array|NotEmpty',
// ...
];
protected $scene = [
// ...
'updateAttrs'=>['id','goods_attrs'],
];
路由 router/admin.php
php
Route::post('goods/attrs/:id','admin.Goods/updateAttrs');