please consider this situation:
model name = Workflow
attribute name = current_user (a foreign key to User model)
then my relation method is:
public function currentUser()
{
return $this->belongsTo(User::class, 'current_user', 'id');
}
and my accessor method is (according to Laravel 9.x Docs):
protected function currentUser(): Attribute
{
return Attribute::make(
get: fn($value) => User::find($value)->full_name(),
set: fn($value) => $value,
);
}
they have the same name, and not working!
also, I tried to change their names but Laravel won't recognize them and returns a null
object.
CodePudding user response:
I found this solution for the accessor method (on Laravel 8.x Docs):
protected function getNextUserAttribute($value)
{
return User::find($value)->full_name();
}
but I'm not sure it's the right way, is it?
CodePudding user response:
I used to use this way
In User
model
protected function getFullNameAttribute($value)
{
return $this->last_name . ' ' . $this->first_name;
}
In Workflow
modal
public function user()
{
return $this->belongsTo(User::class, 'current_user', 'id');
}
$workflow = Workflow::with('user')->where('id', //id)->first();
$fullName = $workflow->user->fullName;