UserC 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\Support\Facades\Hash;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

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;

use App\Http\Requests\UserNewReq;
use App\Http\Requests\UserUpdateReq;

class UserC extends Controller
{
    use AjaxResponse;
    use PermissionCheck;

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

    public function __construct(){
    }

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

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

        $data['page_title'] = '所有管理員';
        return view('admin.' . $this->view_control . '.index', $data);
    }

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

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

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

        $data = User::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'] = 'user';

        $id = $req->input('id');
        $row = User::find($id);
        //logg($row);
        $roles = RoleM::orderBy('name', 'asc')
            ->get();
        $arr = [];
        $user_roles = [];
        foreach ($roles as $key => $item) {
            if($row->hasRole($item->name) == true){
                $user_roles[] = $item->id;
            }

            $arr[$item->id] = $item->name;
        }
        $data['roles'] = $arr;

        $row->roles = $user_roles;
        $data['row'] = $row;
        //logg($user_roles);

        $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'] = 'user';

        $row = new User;
        $row->roles = [];
        $data['row'] = $row;

        $roles = RoleM::orderBy('name', 'asc')
            ->get();
        $arr = [];
        foreach ($roles as $key => $item) {
            $arr[$item->id] = $item->name;
        }
        $data['roles'] = $arr;

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

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

        $data = $req->except(['_token', 'password2']);
        //logg($data);

        $id = $data['id'];
        $user = User::find($id);
        $user->name = $data['name'];
        $user->email = $data['email'];

        //如果修改帳號,要確定帳號不能重複
        if($data['email'] != $user->email){
            $other = User::where('email', $data['email'])
                ->where('id', '!=', $id)
                ->first();
            if($other != false){
                return $this->fail_response('帳號已經有人使用');
            }
        }

        //Validate tag_id
        if($data['tag_id'] != $user->tag_id){
            $result = $user->item_exists('tag_id', $data['tag_id']);
            if($result == true){
                return $this->fail_response('Tag ID已經存在');
            }
        }
        $user->tag_id = $data['tag_id'];

        $password = $data['password'];
        if(isset($data['password']) && $data['password'] != false){
            $user->password = Hash::make($data['password']);
        }

        $user->save();

        $roles = $data['roles'];
        $user->syncRoles($roles);

        return $this->success_response(true);
    }

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

        $data = $req->except(['_token', 'password2']);
        //logg($data);

        $user = new User;
        $user->name = $data['name'];
        $user->email = $data['email'];
        $user->tag_id = $data['tag_id'];
        $user->password = Hash::make($data['password']);
        $user->save();

        $roles = $data['roles'];
        $user->syncRoles($roles);

        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');
        }

        $admin = User::find($id);
        $admin->syncRoles();
        $admin->delete();

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