roles() public method

A permission can be applied to roles.
public roles ( ) : Illuminate\Database\Eloquent\Relations\BelongsToMany
return Illuminate\Database\Eloquent\Relations\BelongsToMany
 /**
  * Method for stroring a new permission in the database.
  *
  * @param PermissionRequest $request
  * @return mixed
  */
 public function store(PermissionRequest $request)
 {
     $permission = new Permission($request->all());
     $permission->save();
     if (is_null($request->get('roles'))) {
         $permission->roles()->sync([]);
     } else {
         $permission->roles()->sync($request->get('roles'));
     }
     $routes = $request->get('routes');
     foreach ($routes as $route) {
         $permission->routes()->create(['route' => $route]);
     }
     return redirect(route('rbac.permissions.index'))->withSuccess('You have successfully created a new permission.');
 }
Beispiel #2
0
 /**
  * Seed Data Source
  *
  * @author SL
  * @return void
  */
 protected function seedDataSource()
 {
     Model::unguard();
     $permission = new \App\Permission();
     $permission->name = 'applications-merchant-payments';
     $permission->display_name = 'Merchant Payments';
     $permission->description = 'merchant payments';
     $permission->save();
     $roles = ['manager', 'su', 'administrator'];
     foreach ($roles as $roleName) {
         try {
             $role = \App\Role::where('name', '=', $roleName)->first();
             $permission->roles()->attach($role->id);
         } catch (Exception $e) {
         }
     }
     $permission->save();
     Model::reguard();
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  App\Permission $permission
  * @return Response
  */
 public function edit($permission)
 {
     $current_roles = array_pluck($permission->roles()->get(['roles.id'])->toArray(), 'id');
     return view('templates.' . \Config::get('webpanel.template') . 'webpanel.panel.permissions.edit', compact('permission', 'current_roles'));
 }