当我更新的类别时skills
,技能记录不会更改。我需要更新与类别相关的所有记录,但是在static::updating()
调用时更新级联不起作用。
技能模型:
<?php
namespace App\Model;
class SkillsModel extends BaseModel
{
protected $table = 'skills';
protected $primaryKey = 'id';
public function categories() {
return $this->hasMany(
SkillsModel::class,
'category_id',
'id');
}
protected static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
static::deleting(function ($skills){
$skills->categories()->delete();
});
static::restored(function ($skills){
$skills->categories()->withTrashed()->restore();
});
static::updating(function ($skills){
$skills->categories()->update();
$s = 1;
});
}
}
***************
private function save($model, $name, $category,$categoryId, $proficiency,$personal_info_id)
{
if ($name != null)
$model->name = $name;
if ($category != null)
$model->category = $category;
if ($proficiency != null)
$model->proficiency = $proficiency;
if ($personal_info_id != null)
$model->personal_info_id = $personal_info_id;
if ($categoryId != null){
$model->category_id = $model->categoryId;
}else{
$model->save();
$model->category_id = $model->id;
}
return $model->save();
}
***************
$isUpdated = $this->save(
$this->skillRepo->find($request->getParam('id')),
null,
$request->getParam('category'),
$request->getParam('categoryId'),
null,
$this->personalInfoRepo->getLastRecordId());
表(此表具有到类别列的自连接):
-- auto-generated definition
create table skills
(
id int auto_increment
primary key,
name varchar(200) null,
category varchar(200) not null,
proficiency int null,
created_at timestamp default CURRENT_TIMESTAMP not null,
updated_at timestamp null,
deleted_at timestamp null,
personal_info_id int not null,
category_id int null,
constraint fk12
foreign key (personal_info_id) references admin_personal_information (id)
on update cascade on delete cascade,
constraint fk13
foreign key (category_id) references skills (id)
on update cascade on delete cascade
);
create index skills_admin_personal_information_index
on skills (personal_info_id);
create index skills_index
on skills (category_id);
我猜想您想使用当前模型的值来更新所有skills
具有category_id
当前记录的记录category
:
$skill->categories()->update(['category' => $skill->category]);
我对你所问的猜测。虽然这不会是递归的事情,但这仅是1级。这是直接在数据库上调用更新,它不涉及模型或模型事件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句