Reputation: 746
I have a users table that contains username and password access data, but this table tries to convert a polymorphic relationship, since the access can belong to different models model1, model2, etc.
migration table users
$table->unsignedInteger('roleable_id');
$table->string('roleable_type');
Model User
public function roleable()
{
return $this->morphTo();
}
public function getFullNameAttribute()
{
return $this->roleable->attributes['lastname'] . ' ' .
$this->roleable->attributes['name'];
}
Others Models
public function rol()
{
return $this->morphOne(App\User::class,'roleable');
}
What I try is to access the model data through the user, but I always return error,
Trying to get property 'attributes' of non-object (View:
What am I doing wrong or is the approach wrong?
View
{{ auth()->user()->full_name }}
Upvotes: 0
Views: 429
Reputation: 11414
The error you're getting suggests that the user isn't associated yet with a roleable
model.
Upvotes: 1
Reputation: 3030
It sounds like there is no role set for the user model. Check the database and make sure that roleable_id and roleable_type have values in those.
Upvotes: 1