mercredi 2 septembre 2015

Eloquent relationship between user roles

I am creating an app where users have different roles. Some of the roles include:

  • Administrator
  • Supervisor
  • Contractor

I have the following tables setup:

  • user
  • role
  • role_user

The role_user is a pivot table used to store the user_id and role_id.

Now, when a Contractor is created, I trigger an event which assigns the user to the Contractor role in the database:

$user->roles()->save($role, ['cust_id' => $event->user->cust_id]);

The above line basically just creates a new row in the role_user pivot table and points to the role_id of a Contractor. This results in the user having Contractor privileges.

Also, within the event I need to assign the user to a Supervisor. However, there is no Supervisor model. Supervisors are also stored in the user table and have a role of Supervisor assigned to them.

How can I create a relationship between Contractors and Supervisors?

Ideally, i'd like to use something like this to create the Contractor/Supervisor relationship in the pivot table:

$user->supervisors()->save($supervisors, ['cust_id' => $event->user->cust_id]);

But i'm not sure how to setup my model...?

/**
 * The supervisors that belong to the user.
 *
 * @return Object
 */
public function supervisors()
{
    return $this->belongsToMany('SimplyTimesheets\Models\User\User')->withTimestamps();
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Xfjjmq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire