%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/silvzytp/dsr_code/app/Http/Controllers/Backend/Member/
Upload File :
Create Path :
Current File : //home/silvzytp/dsr_code/app/Http/Controllers/Backend/Member/RoleController.php

<?php

namespace App\Http\Controllers\Backend\Member;

use App\Models\Role;
use App\Models\Module;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Gate;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // authorized
        Gate::authorize('app.roles.index');

        // page title
        page_title('Roles');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Roles'=>''];
        // roles
        $roles = Role::with('permissions')->whereNotIn('slug',['super-admin','agent','admin'])->latest('id')->get();

        return view('backend.member.pages.roles.index',['roles'=>$roles,'breadcrumb'=>$breadcrumb]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // authorized
        Gate::authorize('app.roles.create');

        // page title
        page_title('New Role');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Roles'=>route('app.roles.index'),'Create'=>''];
        // modules
        $modules = Module::all();
        // roles
        $roles = Role::whereNotIn('slug',['super-admin','agent','admin'])->orderBy('name','asc')->get();
        return view('backend.member.pages.roles.form',['modules'=>$modules,'roles'=>$roles,'breadcrumb'=>$breadcrumb]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // authorized
        Gate::authorize('app.roles.create');

        // validate rules
        $request->validate([
            'role_name' => ['required','string','max:20','unique:roles,name']
        ]);

        Role::create([
            'name' => $request->role_name,
            'slug' => Str::slug($request->role_name)
        ])->permissions()->sync($request->permission);

        return redirect()->route('app.roles.index')->with('success',ALERT_SUCCESS);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function edit(Role $role)
    {
        // authorized
        Gate::authorize('app.roles.edit');

        // page title
        page_title('Edit Role');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Roles'=>route('app.roles.index'),'Edit'=>''];
        // modules
        $modules = Module::all();
        // roles
        $roles = Role::whereNotIn('slug',['super-admin','agent','admin'])->orderBy('name','asc')->get();
        return view('backend.member.pages.roles.form',['modules'=>$modules,'roles'=>$roles,'breadcrumb'=>$breadcrumb,'role'=>$role]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Role $role)
    {
        // authorized
        Gate::authorize('app.roles.edit');

        // validate rules
        $request->validate([
            'role_name' => ['required','string','max:20','unique:roles,name,'.$role->id]
        ]);

        // role update
        $role->update([
            'name' => $request->role_name,
            'slug' => Str::slug($request->role_name)
        ]);

        // permission update
        $role->permissions()->sync($request->permission);

        return back()->with('success',ALERT_UPDATE);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        if ($request->ajax()) {
            Role::findOrFail($id)->delete();
            $output = ['status'=>'success','message'=>ALERT_DELETE];
            return response()->json($output);
        }
    }
}

Zerion Mini Shell 1.0