dimanche 3 décembre 2017

Laravel: Relation between three model

I'm doing a project where every user can be assigned in a designation.

I did my work by using three model: User, Designation & UsersDesignation

The table structures are:

User:
id      name        email       password
---     -----       -----       --------
1       User 1      u1@em.com   kjasdkfjla
2       User 2      u2@em.com   fksdjfghal
3       User 3      u3@em.com   ghhaljaljl

Designation:
id      name
---     -----
1       Faculty
2       Lecturer
3       Lab Instructor

UsersDesignation:
id      userId      designationId
---     ------      -------------
1       1           2
2       2           1
3       3           3

But I'm facing difficulty to get the designation of user from user object by establishing relationship.

I've tried in my Model so far:

User(model):
public function userDesignation()
{
    $this->hasOne('App\UsersDesignation', 'id', 'userId');
}

UserDesignation(model):
public function user()
{
    $this->belongsToMany('App\User', 'userId', 'id');
}
public function designation()
{
    $this->belongsTo('App\Designation', 'designationId', 'id');
}

Actually I want to show the user profiles from user model with his/her designation. But the above way didn't work. I've no idea how to make this relation.

This is my view file:

<div>
    
</div>

The error I get every time

ErrorException (E_ERROR) App\User::userDesignation must return a relationship instance. (View: ....\resources\views\profiles\show.blade.php)

I badly need this help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire