vendredi 25 janvier 2019

How to obtain the polymorphic relationship of a model, return null Laravel 5.6

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 null, What am I doing wrong or is the approach wrong?

View





via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire