Laravel - User > Roles > Permissions within a Company - laravel

I am trying to describe a relationship between a User and a Company. More specific I want the following:
A User has one or Many Roles within a Company.
A Role has one or Many Permissions.
- users (id, name, surname, email, ...)
- permissions (id, name, label, ...)
- roles (id, name, label,...)
- companies (id, name, address, ...)
- permission_role (permission_id, role_id)
- user_role (user_id, role_id)
- company_user (company_id, user_id)
Anyone could help me out?
ER Diagram: https://prnt.sc/q4l614

The pivot_table name will be like user_role_company. Pivot table structure will be like
role_user Table
+---------+-----------+----------+-------------+
| id |user_id | role_id | company_id |
+---------+-----------+----------+-------------+
| 1 | 1 | 2 | 3 |
+---------+-----------+----------+-------------+
One User has one or many company id,
One User has also one or many role id,
One Role has belongs to many user
One Role has belongs to many company.
One Company has belongs to many user.
One Company has belongs to many role.
All of this scenario. you can implements scenario by this -
In User Model
class User extends Authenticatable
{
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function companyies()
{
return $this->belongsToMany(Company::class);
}
}
In Role Model -
class Role extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
public function companies()
{
return $this->belongsToMany(Company::class);
}
}
In Company Model -
class Company extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
Same as for Role and Permission.
You can check the relationship using Laravel tinker.
I Hope it will work for you.
you can read more details from this link : https://laravel.com/docs/6.x/eloquent-relationships#many-to-many

Related

Best Practice to Get Roles of Active Users = Auth::user()

I have 3 table
users
roles
role_users
this table created by Orchid Platform, i want to get role name of Auth::user() in Dashboard Laravel 8 (/home) from role_users table, i have tried to add role_id to users table, thats realy work.
But i want use role_users, because Orchid Platform use role_users.
Please help me, thank you.
I don't know about "Orchid Platform" but that User<->Role (ManyToMany) relationship is a pretty common database structure.
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
User Model:
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
Role Model:
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany(User::class);
}
}
To get roles of the authenticated User
$user = User::find(1);
foreach ($user->roles as $role) {
//
}
If your users will only have 1 role, you can just use a method in your User model to get first role of the User.
public function getRole(){
return $this->roles()->first();
}
Now, you can used this way:
Auth::user()->getRole()->name

Laravel relationship for user with multiple scenarios

So, i need to make relationship between users table, roles and sections table, the table structure as follows :
users
id - integer
name - string
role_id - foreignId
section_id - foreignId
sections
id - integer
section - string
department - string
roles
id - integer
title - string
User model :
<?php
namespace App\Models;
class User extends Authenticatable
{
use HasFactory, Notifiable;
public function section()
{
return $this->belongsTo(Section::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
}
The model above is OK if the user only belongsTo one section.
The problem is user can belongs to more than one section depending on their role.
The role itself determine this matrix below
How can i achive this ?

Relationship between user and store

I need to create a relationship between user and many stores. I have created a three models
Store Model
id name email phone info
1 xyz xyz#gmail.com 9329292922 Small Store
2 abc abc#gmail.com 9494949449 Some Store
User Model
id name email
1 ewd ewd#gmail.com
2 xcv xcv#gmail.com
User_Store
user_id store_id
1 1
1 2
What does the user_store model contain relations whether it is belongstoMany or hasmany?
You can use belongsToMany relationship
In your Store model define a method
public function users() {
return $this->belongsToMany(Users::class, 'user_store', 'user_id', 'store_id');
}
In your Users model define a method
public function stores() {
return $this->belongsToMany(Stores::class, 'user_store', 'user_id', 'store_id');
}
Looks to me like you want a simple many-to-many relationship.
For this you only need two models User and Store, you only need StoreUser if you want to do something special with the pivot table otherwise it is unnecessary.
The following would be the Laravel way:
Table structure
stores
id
name
email
phone
info
users
id
name
email
store_user
user_id
store_id
Laravel excepts the pivot table to be called store_user, you can read more about it here:
To define this relationship, three database tables are needed: users,
roles, and role_user. The role_user table is derived from the
alphabetical order of the related model names, and contains the
user_id and role_id columns.
Model structure
class User extends Model
{
public function stores()
{
return $this->belongsToMany(Store::class);
}
}
class Store extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
}

I want to make many to many relationship using laravel Model

I am trying to make many to many relationships b/w three table I don't know how to make that please confirm me my relationship correct or not.
Project table
id | name
user table
id | name
project_assign
id | user_id | project_id
User Model
public function project_assign()
{
return $this->belongsToMany('App\Project_assign','project_assign','user_id','id');
}
project_assign Model
public function user()
{
return $this->belongsToMany('App\User','Project_assign','user_id','id');
}
Project Model
public function project_assign()
{
return $this->belongsToMany('App\Project_assign','project_assign','project_id','id');
}
you don't need to make a relation in project_assign model. only Project and User will have relation,
Project Model
public function users(){
return $this->belongsToMany('App\User','project_assign','project_id','user_id');
}
User Model
public function projects(){
return $this->belongsToMany('App\Project','project_assign','user_id','project_id');
}
In controller you can get like this
User::with('projects')->get();
Project::with('users')->get();

Laravel: how order by many to many table?

I have tables
users
id
username
role
id
name
users_role
user_id
role_id
I have users listing and I want ability to sort by username and role_id, how I can do this?
users
1:first
2:second
3:third
roles
1:admin
2:customer
users_role
1:1
1:2
2:2
3:2
Sort can be by first user role_id(or for max role_id -> if user have roles 1 and 2 sort by 2)
First of all, you need to create relationships for two model: User and Role (i suppose that we have three tables: users, roles and role_user:
class User extends Model
{
public function roles() {
return $this->belongsToMany('\App\Role');
}
}
class Role extends Model
{
public function users()
{
return $this->belongsToMany('\App\User');
}
}
Next, with this query you can have list of users and roles, sort by username ASC and role_id DESC
\App\User::with(array('roles' => function($query) {
$query->orderBy('role_id', 'DESC');
}))
->orderBy('username', 'ASC')
->get();

Resources