I have a form to edit my users,
<form action="{{ route('user.update', ['id' => $user->id]) }}" method="post">
@csrf
@method('PUT')
--content--
</form>
A file web.php with all the routes where I have the get method to receive the user’s informations
//CRUD Edit Users
Route::get('dashboard/crud/users/{id}/edit/', [AppHttpControllersUserCrudController::class, 'editUsers'])->middleware(isAdmin::class);
Route::put('dashboard/crud/users/{id}/edit/', [AppHttpControllersUserCrudController::class, 'editUsersPost'])->middleware(isAdmin::class)->name('user.update');
And a controller with a function to edit my user, I added a ‘dd’ to see if I’m calling the method but it never does
Public function editUsersPost(ProfileUpdateRequest $request, $id): RedirectResponse
{
dd('123');
$user = User::findOrFail($id);
$user->fill($request->validated());
if ($user->isDirty('email')) {
$user->email_verified_at = null;
}
$user->save();
return Redirect::route('dashboard.crud.users')->with('status', 'profile-updated');
}
This is the profileUpdateRequest Class
class ProfileUpdateRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, IlluminateContractsValidationRule|array|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
];
}
2
Answers
Rewrite your code like the following one:
use IlluminateHttpRequest; Public function editUsersPost(Request $request, $id){}