RoleC for Laravel-Permission

Example

<?php
namespace App\Http\Controllers\Admin\Permission;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\ParentC;

use Carbon\Carbon;

use App\User;
use App\Models\RoleM;

use App\Traits\AjaxResponse;
use App\Traits\PermissionCheck;

//use App\Mail\B2bRegMail;

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;


class RoleC extends Controller
{
    use AjaxResponse;
    use PermissionCheck;

    protected $controller = 'role';
    protected $pms_key = 'role ';
    protected $view_control = 'role';

    public function __construct(){
        //parent::__construct();
    }

    protected function guard(){
        return Auth::guard('web');
    }

    public function index(Request $req){
        $this->check_pms($this->pms_key .'view');
        $data['controller'] = $this->controller;
        $data['menu_key'] = 'role';
        $user = $this->guard()->user();
        //logg($data);

        $data['page_title'] = '角色';
        return view('admin.' . $this->view_control . '.index', $data);
    }

    public function all_data(Request $req){
        //logg($req->input());
        $draw = $req->input('draw');
        $start = $req->input('start');
        $length = $req->input('length');
        $search = $req->input('search');
        $keyword = $search['value'];

        $sorting = RoleM::get_sorting($req);
        //logg($sort);

        $options = [
            'start' => $start, 
            'limit' => $length, 
            'sort' => $sorting, 
            'keyword' => $keyword, 
        ];
        $total = RoleM::items($options, RoleM::DT_TOTAL);
        $filtered_total = RoleM::items($options, RoleM::DT_FILTERED);
        $data = RoleM::items($options);

        $data = RoleM::datatable_data($data);

        return $this->datatable_response($total, $filtered_total, $data, $draw);
    }

    public function edit_item(Request $req){
        $this->check_pms($this->pms_key .'edit');
        $data['controller'] = $this->controller;
        $data['menu_key'] = 'role';
        $user = $this->guard()->user();

        $id = $req->input('id');
        $role = Role::find($id);
        $data['row'] = $role;

        $pms = Config::get('admin.role_permissions');
        $data['pms'] = $pms;

        $role_pms = $role->getPermissionNames();
        $data['role_pms'] = $role_pms->toArray();
        //logg($role_pms);

        $data['page_title'] = '修改角色';
        return view('admin.' . $this->view_control . '.edit', $data);
    }

    public function create_item(Request $req){
        $this->check_pms($this->pms_key .'edit');
        $data['controller'] = $this->controller;
        $data['menu_key'] = 'role';
        $user = $this->guard()->user();

        $data['row'] = new Role;
        $data['role_pms'] = [];

        $pms = Config::get('admin.role_permissions');
        $data['pms'] = $pms;

        $data['page_title'] = '新增角色';
        return view('admin.' . $this->view_control . '.create', $data);
    }

    public function edit_save(Request $req){
        $this->check_pms($this->pms_key .'edit', true);

        $id = $req->input('id');
        $name = $req->input('name');
        $pms = $req->input('pms');

        if(is_numeric($name) == true){
            return $this->fail_response('角色名稱不可為數字');
        }

        $role = Role::find($id);
        $role->name = $name;
        $role->save();

        //刪除role的所有permission
        $role->syncPermissions();

        if($pms != false){
            foreach ($pms as $pname => $values) {
                foreach ($values as $key => $value) {
                    $permission = Permission::firstOrCreate(['name' => $pname . ' ' . $value]);
                    $permission->assignRole($role);
                }
            }
        }

        return $this->success_response(true);
    }

    public function create_save(Request $req){
        $this->check_pms($this->pms_key .'edit', true);

        $name = $req->input('name');
        if($name == false){
            return $this->fail_response('請輸入名稱');
        }elseif(is_numeric($name) == true){
            return $this->fail_response('角色名稱不可為數字');
        }

        $pms = $req->input('pms');

        $role = new Role();
        $role->name = $name;
        $role->save();

        if($pms != false){
            foreach ($pms as $pname => $values) {
                foreach ($values as $key => $value) {
                    $permission = Permission::firstOrCreate(['name' => $pname . ' ' . $value]);
                    $permission->assignRole($role);
                }
            }
        }

        return $this->success_response(true);
    }

    public function delete_item(Request $req){
        $this->check_pms($this->pms_key .'delete', true);

        $id = $req->input('id');
        if($id == false){
            return $this->fail_response('Invalid ID');
        }

        $role = Role::find($id);

        $users = User::role($role->name)->get();
        foreach ($users as $key => $user) {
            
        }

        //刪除role的所有permission
        $role->syncPermissions();

        $role->delete();

        return $this->success_response(true);
    }
}