Don't mix relationships and Attributes; in fact, better if you don't name an Attribute/Mutator the same as your relationship name!
Jun 15, 2023
3
Level 14
New attribute casting and relationships
Well, let's say I have a relationship:
public function file(): HasOne
{
return $this->hasOne(File::class, 'id', 'file_id');
}
But I also need a setter for file. In Laravel 8 and below I did it like this:
public function setFileAttribute(array $value = null): void
{
$this->attributes['file_id'] = $value ? $value['id'] : null;
}
Now it looks like this:
public function setFileAttribute(array $value = null): void
{
$this->attributes['file_id'] = $value['id'] ?? null;
}
Yeah, not so much difference :) But I want to completely remove this and use new attribute casting, but how to do it? I tried something like this:
public function file(): Attribute
{
return Attribute::make(
get: fn (): HasOne => $this->hasOne(File::class, 'id', 'file_id'),
set: fn (array $value = null): array => [
'file_id' => $value['id'] ?? null,
]
);
}
But it doesn't work. Any idea how to implement this?
Please or to participate in this conversation.