master_template/app/Http/Controllers/UsersController.php
2024-06-23 17:02:56 +05:45

138 lines
3.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Repositories\RoleInterface;
use App\Repositories\RoleRepository;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Brian2694\Toastr\Facades\Toastr;
class UsersController extends Controller
{
protected $userRepository;
protected $employeeRepository;
protected $roleRepository;
public function __construct(UserRepository $userRepository, RoleRepository $roleRepository)
{
$this->userRepository = $userRepository;
$this->roleRepository = $roleRepository;
}
public function index()
{
$data['users'] = $this->userRepository->findAll();
$data['editable'] = false;
$data['roleLists'] = $this->roleRepository->pluck();
return view('crud.generated.users.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = "Create User";
$data['editable'] = false;
$data['roleLists'] = $this->roleRepository->pluck();
return view('crud.generated.users.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
// try {
$validated = $request->validate([
'name' => 'required|min:5',
'email' => 'required',
'password' => 'required',
]);
$validated['password'] = bcrypt($validated['password']);
$this->userRepository->create($validated, $request->role);
Toastr::success('User has been created successfully.', 'Title', ["positionClass" => "toast-top-center"]);
// } catch (\Throwable $th) {
// echo $th->getMessage();
// toastr()->error($th->getMessage());
// }
return redirect()->route('user.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['user'] = $this->userRepository->getUserById($id);
return view('user::user.show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = "Edit User";
$data['editable'] = true;
$data['roleLists'] = $this->roleRepository->pluck();
$data['user'] = $this->userRepository->getUserById($id);
return view('crud.generated.users.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
try {
$validated = $request->validate([
'name' => 'required|min:5',
'email' => 'required',
'password' => 'required',
]);
$validated['password'] = bcrypt($validated['password']);
$this->userRepository->update($id, $validated, $request->role);
Toastr::success('User has been updated', 'Title', ["positionClass" => "toast-top-center"]);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('user.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->userRepository->delete($id);
Toastr::success('User has been deleted', 'Title', ["positionClass" => "toast-top-center"]);
} catch (\Throwable $th) {
Toastr::success('Error deleting user', 'Title', ["positionClass" => "toast-top-center"]);
}
}
}