<?php
use App\Permission;
use App\Role;
use Illuminate\Database\Seeder;
class PermissionsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//get first three roles
$roles = Role::orderBy('id', 'asc')->take(1)->get();
$permissions = [
['name' => 'view.roles', 'display_name' => 'View Roles', 'description' => 'View the Role', 'group_key' => 'Roles'],
['name' => 'create.roles', 'display_name' => 'Create Roles', 'description' => 'Create the Role', 'group_key' => 'Create Roles'],
['name' => 'edit.roles', 'display_name' => 'Edit Roles', 'description' => 'Edit the Role', 'group_key' => 'Edit Roles'],
['name' => 'delete.roles', 'display_name' => 'Delete Roles', 'description' => 'Delete the Role', 'group_key' => 'Delete Roles'],
['name' => 'view.users', 'display_name' => 'View Users', 'description' => 'View the Users', 'group_key' => 'Users'],
['name' => 'create.users', 'display_name' => 'Create Users', 'description' => 'Create User', 'group_key' => 'Create Users'],
['name' => 'edit.users', 'display_name' => 'Edit Users', 'description' => 'Edit User', 'group_key' => 'Edit Roles'],
['name' => 'delete.users', 'display_name' => 'Delete Users', 'description' => 'Delete roles', 'group_key' => 'Delete Roles'],
];
//create all permissions
foreach ($permissions as $permission)
{
Permission::create($permission);
}
//get all permissions
$permi = Permission::pluck('id')->toArray();
//assign all permissions to three roles.
foreach($roles as $role) {
$role->permissions()->attach($permi);
<?php
use App\Role;
use App\User;
use Illuminate\Database\Seeder;
class AssignRoleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$user = User::findOrFail(1)->first();
$role = Role::findOrFail(1)->first();
return $user->roles()->attach($role);
}
}
<?php
use App\Role;
use Illuminate\Database\Seeder;
class RolesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//Roles
$roles = [
[
'name' => 'admin',
'display_name' => 'Admin',
'description' => 'Adminstrator',
],
[
'name' => 'author',
'display_name' => 'Author',
'description' => 'Author',
],
[
'name' => 'manager',
'display_name' => 'Manager',
'description' => 'Manager',
],
];
foreach ($roles as $role){
Role::create($role);
}
}
}
<?php
use App\RoleUser;
use Illuminate\Database\Seeder;
class RoleUserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
RoleUser::create([
'user_id' => 1,
'role_id' =>1,
]);
}
}
<?php
use App\User;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//Create User
User::create([
'name' => 'Ben',
'email'=>'admin:gmail.com',
'password'=> bcrypt('password'),
'status' => '1',
]);
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('display_name');
$table->string('description')->nullable();
$table->softDeletes();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('display_name');
$table->string('description')->nullable();
$table->string('group_key')->nullable();
$table->softDeletes();
$table->timestamps();
});
/**
* This table will hold permission for particular role
*/
Schema::create('permission_role', function (Blueprint $table) {
$table->bigInteger('permission_id')->unsigned();
$table->bigInteger('role_id')->unsigned();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->primary(['permission_Id', 'role_id']);
});
/**
* This table will hold the roles for users and role id
* you can assign more than one role for each user now
*/
Schema::create('role_user', function (Blueprint $table) {
$table->bigInteger('role_id')->unsigned();
$table->bigInteger('user_id')->unsigned()->nullable();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('permission_role');
Schema::dropIfExists('role_user');
Schema::dropIfExists('permissions');
Schema::dropIfExists('roles');
}
}
class Permission extends Model
{
protected $table = 'permissions';
public $fillable =['name','display_name','description','group_key'];
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PermissionRole extends Model
{
protected $table = 'permission_role';
public $timestamps = false;
protected $fillable = [
'permission_id',
'role_id',
];
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public $fillable =['name','display_name','description'];
public function permissions()
{
return $this->hasMany(Permission::class);
}
public function users()
{
return $this->belongsToMany(User::class);
}
public function Role_Users()
{
return $this->belongsToMany(Role_user::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class RoleUser extends Model
{
protected $table = 'role_user';
public $timestamps = false;
protected $fillable = [
'user_id',
'role_id'
];
public function user()
{
return $this->belongsToMany(User::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
}
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function Role_user()
{
return $this->hasOne(Role_user::class);
}
}
}
}
}