3. 4 Edit Role (ok)

C:\xampp82\htdocs\phongkhamnet\app\Http\Controllers\RoleController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use DB;
class RoleController extends Controller
{
  /**
   * Display a listing of the resource.
   */
  public function index()
  {
    $roles = Role::paginate(2);
    return view('roles.index')->with(compact('roles'));
  }
  /**
   * Show the form for creating a new resource.
   */
  public function create()
  {
    $permission = Permission::get();
    return view('roles.create', compact('permission'));
  }
  /**
   * Store a newly created resource in storage.
   */
  public function store(Request $request)
  {
    $this->validate($request, [
      'name' => 'required|unique:roles,name',
      'permission' => 'required',
    ]);
    $role = Role::create(['name' => $request->input('name')]);
    $role->syncPermissions($request->input('permission'));
    return redirect()->route('role.index');
  }
  /**
   * Display the specified resource.
   */
  public function show(string $id)
  {
    $role = Role::find($id);
    $rolePermissions = Permission::join("role_has_permissions", "permissions.id", "=", "role_has_permissions.permission_id")
      ->where("role_has_permissions.role_id", $id)->get();
    return view('roles.show')->with(compact('role', 'rolePermissions'));
  }
  /**
   * Show the form for editing the specified resource.
   */
  public function edit(string $id)
  {
    $role = Role::find($id);
    $permission = Permission::get();
    $rolePermissions = DB::table("role_has_permissions")
      ->where("role_has_permissions.role_id", $id)
      ->pluck('role_has_permissions.permission_id', 'role_has_permissions.permission_id')
      ->all();
    return view('roles.edit', compact('role', 'permission', 'rolePermissions'));
  }
  /**
   * Update the specified resource in storage.
   */
  public function update(Request $request, string $id)
  {
    $role = Role::find($id);
    $role->name = $request->input('name');
    $role->save();
    $role->syncPermissions($request->input('permission'));
    return redirect()->route('role.index');
  }
  /**
   * Remove the specified resource from storage.
   */
  public function destroy(string $id)
  {
    //
  }
}

C:\xampp82\htdocs\phongkhamnet\resources\views\roles\edit.blade.php

@extends('layouts.app')
@section('content')
<div class="row">
  <div class="col-lg-12 margin-tb">
    <div class="pull-left">
      <h2>Edit Role</h2>
    </div>
    <div class="pull-right">
      <a class="btn btn-primary" href="{{ route('role.index') }}"> Back</a>
    </div>
  </div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
  <strong>Whoops!</strong> There were some problems with your input.<br><br>
  <ul>
    @foreach ($errors->all() as $error)
    <li>{{ $error }}</li>
    @endforeach
  </ul>
</div>
@endif
{!! Form::model($role, ['method' => 'PATCH','route' => ['role.update', $role->id]]) !!}
<div class="row">
  <div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
      <strong>Name:</strong>
      {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
    </div>
  </div>
  <div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
      <strong>Permission:</strong>
      <br />
      @foreach($permission as $value)
      <label>{{ Form::checkbox('permission[]', $value->id, in_array($value->id, $rolePermissions) ? true : false,
        array('class' => 'name')) }}
        {{ $value->name }}</label>
      <br />
      @endforeach
    </div>
  </div>
  <div class="col-xs-12 col-sm-12 col-md-12 text-center">
    <button type="submit" class="btn btn-primary">Submit</button>
  </div>
</div>
{!! Form::close() !!}
@endsection

Last updated