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.
82 lines
2.0 KiB
PHP
82 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
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::orderBy('id');
|
|
|
|
if ($request->q != null) {
|
|
$query->where('name', 'like', '%'.$request->q.'%');
|
|
}
|
|
|
|
return inertia('User/Index', [
|
|
'users' => $query->paginate(10),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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',
|
|
]);
|
|
|
|
User::create($request->only(['name', 'email', 'password']));
|
|
|
|
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',
|
|
]);
|
|
|
|
$user->update($request->only(['name', 'email']));
|
|
if ($request->password != null) {
|
|
$user->update(['password' => $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();
|
|
}
|
|
}
|