vendredi 30 août 2019

Eloquent model - relationship user category and account handling

I have a particular scenario whereby a user can belong to another user, a group or a service provider.

For eg. a Company (is a user/service provider) has a client (also a user) and a client may have member (group of users) and a group of users ( as per membership modal).

What is the best way to use eloquent model for such scenario.

Thanks in advance.

I have tried user role as per acedmind on youtube as below code.

class Role extends Model
{
//Roles Model
//relationship with users
public function users()
{
return $this->belongsToMany('App\User','user_role','role_id','user_id');
}
}

and in User
//Roles relationship
public function roles()`enter code here`
{
return $this->belongsToMany('App\Role','user_role','user_id','role_id');
}

public function hasAnyRole($roles)
{
  if(is_array($roles)){
 foreach ($roles as $role) {
   if ($this->hasRole($role)){
   return true;
 }
 }
 }else {
  if ($this->hasRole($roles)){
return true;
  }
}
 return false;
}

public function hasRole($role)
{
  if ($this->roles()->where('name',$role)->first()){
return true;
  }
  return false;
 }

  1. a User can be a service provider. (can belong to a group)
  2. a user can be a client as individual and/or can belong to a group
  3. a group can have types of service provider or a client
  4. a service provider has investigation records for each client which will be reviewed as an individual or a group.


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2UiLncR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire