You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.5 KiB
PHP
98 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = User::with('role')->orderBy('id');
|
|
|
|
if ($request->q != null) {
|
|
$query->where('name', 'like', '%'.$request->q.'%');
|
|
}
|
|
|
|
return inertia('User/Index', [
|
|
'users' => $query->paginate(10),
|
|
'roles' => Role::all(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string',
|
|
'email' => 'required|email|unique:users,email',
|
|
'password' => 'required|string|min:6',
|
|
'role_id' => 'required|exists:roles,id'
|
|
]);
|
|
|
|
User::create([
|
|
'name' => $request->name,
|
|
'email' => $request->email,
|
|
'password' => bcrypt($request->password),
|
|
'role_id' => $request->role_id,
|
|
'group' => $request->group,
|
|
'region' => $request->region
|
|
]);
|
|
|
|
return redirect()->route('users.index');
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, User $user)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string',
|
|
'email' => 'required|email|unique:users,email,'.$user->id,
|
|
'password' => 'nullable|string|min:6',
|
|
]);
|
|
|
|
if ($user->is_admin == 0) {
|
|
$request->validate([
|
|
'role_id' => 'required|exists:roles,id'
|
|
]);
|
|
}
|
|
|
|
$user->update($request->only(['name', 'email', 'role_id', 'group', 'region']));
|
|
if ($request->password != null) {
|
|
$user->update(['password' => bcrypt($request->password)]);
|
|
}
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(User $user)
|
|
{
|
|
$user->delete();
|
|
return redirect()->back();
|
|
}
|
|
}
|