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.
monitor-doc/app/Http/Controllers/UserController.php

86 lines
2.1 KiB
PHTML

2 years ago
<?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([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
]);
2 years ago
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' => bcrypt($request->password)]);
2 years ago
}
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();
}
}