edit user functionality
This commit is contained in:
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\UpdateUserRequest;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
@@ -20,4 +22,32 @@ class UserController extends Controller
|
|||||||
'users' => $users,
|
'users' => $users,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the form for editing the specified user.
|
||||||
|
*/
|
||||||
|
public function edit(User $user): View
|
||||||
|
{
|
||||||
|
return view('user.edit', [
|
||||||
|
'user' => $user,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified user.
|
||||||
|
*/
|
||||||
|
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
||||||
|
{
|
||||||
|
$user->fill($request->validated());
|
||||||
|
|
||||||
|
if ($user->isDirty('email')) {
|
||||||
|
$user->email_verified_at = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->route('user.edit', $user)
|
||||||
|
->with('status', 'user-updated');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
app/Http/Requests/UpdateUserRequest.php
Normal file
37
app/Http/Requests/UpdateUserRequest.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class UpdateUserRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => ['required', 'string', 'max:255'],
|
||||||
|
'email' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'email',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('users', 'email')->ignore($this->route('user')),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
46
resources/views/user/edit.blade.php
Normal file
46
resources/views/user/edit.blade.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<x-app-layout>
|
||||||
|
<x-slot name="header">
|
||||||
|
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||||
|
{{ __('Edit User') }}
|
||||||
|
</h2>
|
||||||
|
</x-slot>
|
||||||
|
|
||||||
|
<div class="py-12">
|
||||||
|
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||||
|
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||||
|
<div class="p-6 text-gray-900">
|
||||||
|
@if (session('status') === 'user-updated')
|
||||||
|
<div class="mb-4 rounded-md bg-green-50 px-4 py-3 text-sm text-green-700">
|
||||||
|
{{ __('User updated successfully.') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<form method="POST" action="{{ route('user.update', $user) }}" class="space-y-6 max-w-xl">
|
||||||
|
@csrf
|
||||||
|
@method('patch')
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<x-input-label for="name" :value="__('Name')" />
|
||||||
|
<x-text-input id="name" class="mt-1 block w-full" type="text" name="name" :value="old('name', $user->name)" required autofocus autocomplete="name" />
|
||||||
|
<x-input-error class="mt-2" :messages="$errors->get('name')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<x-input-label for="email" :value="__('Email')" />
|
||||||
|
<x-text-input id="email" class="mt-1 block w-full" type="email" name="email" :value="old('email', $user->email)" required autocomplete="username" />
|
||||||
|
<x-input-error class="mt-2" :messages="$errors->get('email')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center gap-4">
|
||||||
|
<x-primary-button>{{ __('Save') }}</x-primary-button>
|
||||||
|
|
||||||
|
<a href="{{ route('user.index') }}" class="text-sm text-gray-600 underline hover:text-gray-900">
|
||||||
|
{{ __('Back') }}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-app-layout>
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
|
||||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
|
||||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
|
||||||
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody class="divide-y divide-gray-200">
|
<tbody class="divide-y divide-gray-200">
|
||||||
@@ -26,10 +27,15 @@
|
|||||||
<td class="px-4 py-3 text-sm text-gray-900">{{ $user->name }}</td>
|
<td class="px-4 py-3 text-sm text-gray-900">{{ $user->name }}</td>
|
||||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->email }}</td>
|
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->email }}</td>
|
||||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->created_at?->format('Y-m-d H:i') }}</td>
|
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->created_at?->format('Y-m-d H:i') }}</td>
|
||||||
|
<td class="px-4 py-3 text-sm text-gray-700">
|
||||||
|
<a href="{{ route('user.edit', $user) }}" class="font-medium text-indigo-600 hover:text-indigo-500">
|
||||||
|
{{ __('Edit') }}
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@empty
|
@empty
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" class="px-4 py-6 text-sm text-center text-gray-500">
|
<td colspan="5" class="px-4 py-6 text-sm text-center text-gray-500">
|
||||||
No users found.
|
No users found.
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ Route::get('/dashboard', function () {
|
|||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
Route::get('/user', [UserController::class, 'index'])->name('user.index');
|
Route::get('/user', [UserController::class, 'index'])->name('user.index');
|
||||||
|
Route::get('/user/{user}/edit', [UserController::class, 'edit'])->name('user.edit');
|
||||||
|
Route::patch('/user/{user}', [UserController::class, 'update'])->name('user.update');
|
||||||
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
||||||
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
||||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||||
|
|||||||
44
tests/Feature/UserTest.php
Normal file
44
tests/Feature/UserTest.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
test('user edit page requires authentication', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->get("/user/{$user->id}/edit")->assertRedirect('/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can view the user edit page', function () {
|
||||||
|
$actingUser = User::factory()->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$response = $this
|
||||||
|
->actingAs($actingUser)
|
||||||
|
->get("/user/{$user->id}/edit");
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertSee('Edit User')
|
||||||
|
->assertSee($user->name)
|
||||||
|
->assertSee($user->email);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can update a user', function () {
|
||||||
|
$actingUser = User::factory()->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$response = $this
|
||||||
|
->actingAs($actingUser)
|
||||||
|
->patch("/user/{$user->id}", [
|
||||||
|
'name' => 'Updated User',
|
||||||
|
'email' => 'updated@example.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertRedirect("/user/{$user->id}/edit");
|
||||||
|
|
||||||
|
$user->refresh();
|
||||||
|
|
||||||
|
$this->assertSame('Updated User', $user->name);
|
||||||
|
$this->assertSame('updated@example.com', $user->email);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user