Current File : //home/tradevaly/www/app/Http/Controllers/Admin/RoleController.php |
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\Role;
use App\Models\Admin;
use App\Models\Permission;
use App\Models\RoleHasPermission;
use DataTables;
use Illuminate\Http\Request;
class RoleController extends Controller
{
public function index(Request $request){
$data['roles'] = Role::latest()->with('get_permissions')->get();
return view('backend.pages.userManagement.role.index')->with($data);
}
public function create(){
$data['permissions'] = Permission::all();
return view('backend.pages.userManagement.role.create')->with($data);
}
public function store(Request $request){
$request->validate([
'name' => 'required',
'permissions' => 'required',
]);
$role = new Role();
$role->name =$request->name;
$role->save();
foreach($request->permissions as $permission){
$role_has_permission = new RoleHasPermission();
$role_has_permission->permission_id = $permission ;
$role_has_permission->role_id = $role->id ;
$role_has_permission->route_name = Permission::find($permission)->route_name ;
$role_has_permission->save();
}
Toastr::success('success!','Role Created Successfully!!');
return back();
}
public function show($id){
$data['permissions'] = Permission::all();
$data['role'] = Role::findOrFail($id);
return view('backend.pages.userManagement.role.show')->with($data);
}
public function update(Request $request,$id){
if($id == 1){
return abort(404);
}
$role = Role::findOrFail($id);
$role->name =$request->name;
RoleHasPermission::where('role_id',$id)->delete();
foreach($request->permissions as $permission){
$role_has_permission = new RoleHasPermission();
$role_has_permission->permission_id = $permission ;
$role_has_permission->role_id = $role->id ;
$role_has_permission->route_name = Permission::find($permission)->route_name;
$role_has_permission->save();
}
$role->save();
Toastr::success('success!','Role Updated Successfully!!');
return back();
}
public function destroy($id){
if($id == 1){
return abort(404);
}
$role = Role::findOrfail($id);
Admin::where('role_id',$id)->delete();
RoleHasPermission::where('role_id',$id)->delete();
$role->delete();
Toastr::error('Deleted!','Role Deleted Successfully!!');
return back();
}
public function admin_index(){
$data['admins'] = Admin::latest()->get();
return view('backend.pages.userManagement.admin.index')->with($data);
}
public function admin_create(){
$data['roles'] = Role::latest()->get();
return view('backend.pages.userManagement.admin.create')->with($data);
}
public function admin_store(Request $request){
$request->validate([
'name' => ['required', 'string', 'max:255'],
'role_id' => ['required'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:admins'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
$admin = new Admin();
$admin->name = $request->name;
$admin->email = $request->email;
$admin->role_id = $request->role_id;
$admin->phone = $request->phone;
$admin->password = bcrypt($request->password);
$admin->save();
Toastr::success('success!','User Created Successfully!!');
return back();
}
public function admin_destroy($id){
Admin::findOrFail($id)->delete();
Toastr::error('Deleted!','Admin Deleted Successfully!!');
return back();
}
}