Friday, January 28, 2022

[FIXED] POST Method in route gives error 404 - Laravel

Issue

I want to generate a random password on button click but it leads to Error 404

The Form

<form style="position: relative; left: -15px" action="{{ route("dashboard.users.generate-app-password-store") }}" method="POST">                       
   @csrf

    <button type="submit" class="btn btn-primary float-right mt-2">
        {{ trans("translation.generate-password") }}
    </button>
</form>

The Controller Function

public function generateAppEmailPasswordStore(Request $request)
{
    $user = User::findOrFail($request->uid);

    $user->app_email_password_store = Hash::make(Str::random(8));
    
    $user->app_email_password_store = $request->app_email_password_store;
    
    $user->save();  
    
    Alert::flash(trans('translation.email-account-created'));

    return redirect()->route('dashboard.users.profile', ['id' => $user->id]);
}

The route (prefix => dashboard is the main route group)

Route::group(['prefix' => 'user', 'middleware' => 'checkRole:admin'], function () {
    Route::post('/generate-app-password-store', 'Dashboard\UsersController@generateAppEmailPasswordStore')->name('dashboard.users.generate-app-password-store');
});

The error enter image description here


Solution

You are probably recieving 404 because of this line in the controller method.

$user = User::findOrFail($request->uid)

Check if you get the correct value for $request->uid or if there is a user with the given ID.

Otherwise it will throw a 404 error.



Answered By - maazin

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.