123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Role;
- use App\Models\RoleTranslation;
- class RoleController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $roles = Role::paginate(10);
- return view('backend.staff.staff_roles.index', compact('roles'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- return view('backend.staff.staff_roles.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- if($request->has('permissions')){
- $role = new Role;
- $role->name = $request->name;
- $role->permissions = json_encode($request->permissions);
- $role->save();
- $role_translation = RoleTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'role_id' => $role->id]);
- $role_translation->name = $request->name;
- $role_translation->save();
- flash(translate('Role has been inserted successfully'))->success();
- return redirect()->route('roles.index');
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(Request $request, $id)
- {
- $lang = $request->lang;
- $role = Role::findOrFail($id);
- return view('backend.staff.staff_roles.edit', compact('role','lang'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $role = Role::findOrFail($id);
- if($request->has('permissions')){
- if($request->lang == env("DEFAULT_LANGUAGE")){
- $role->name = $request->name;
- }
- $role->permissions = json_encode($request->permissions);
- $role->save();
- $role_translation = RoleTranslation::firstOrNew(['lang' => $request->lang, 'role_id' => $role->id]);
- $role_translation->name = $request->name;
- $role_translation->save();
- flash(translate('Role has been updated successfully'))->success();
- return redirect()->route('roles.index');
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $role = Role::findOrFail($id);
- foreach ($role->role_translations as $key => $role_translation) {
- $role_translation->delete();
- }
- Role::destroy($id);
- flash(translate('Role has been deleted successfully'))->success();
- return redirect()->route('roles.index');
- }
- }
|