Is it possible to use a mutator to set an attribute on a relationship?
I have a model with the following relationship:
public function connection_points() {
return $this->hasMany(CablePair::class);
}
I have an accessor which is using this relationship to access an attribute in the relationship
public function status(): Attribute {
return Attribute::make(
get: fn ($value) => $this->connection_points->first()?->cable_pair_status->name
);
I have also added this field to the $appends array on the model:
protected $appends = [
'status'
]
Now, is it possible to use a mutator to manipulate the relationship attribute like this:
public function status(): Attribute {
return Attribute::make(
get: fn ($value) => $this->connection_points->first()?->cable_pair_status->name,
set: fn ($value) => $this->connection_points->each(function ($item) use ($value) {
$item->cable_pair_status_id = $value;
})
);
}
when I try to save the model it throws an error (understandably) saying that there is no status attribute on the model ....
I can achieve the update outside of the model just by using the relationship model directly like this:
CablePair::where('cable_id', $cable_id)->get()->each(function ($item) use ($new_value) {
$item->cable_pair_status_id = $new_value;
$item->save();
})
I just feel that it is more elegant to do it in the related model itself ...