Reputation: 617
When I update the category of skills
, the skill records won't be changed. I need to update all the records which are related to a category but update cascading doesn't work when static::updating()
is called.
SkillModel:
<?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());
the Table ( this table has a self join to the category column):
-- 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);
Upvotes: 0
Views: 1235
Reputation: 50501
I will guess that you want to update all the skills
that have the category_id
of the current record to be updated with the value of category
of the current model:
$skill->categories()->update(['category' => $skill->category]);
My guess of what you are asking about. This isn't going to be a recursive thing though, this is only 1 level. This is directly calling update on the database, it does not involve the models or model events.
Upvotes: 1