Search code examples
laravelcontrollerrouteslaravel-5.3

Laravel: Route returns 404 page on form submit


SOLVED

web.php route:

Route::post('/admin/users/action', [
    'uses' => 'UserController@massAction',
    'as' => 'user.massAction'
]);

form:

<form id="users-form" role="form" method="POST" action="{{ url('/admin/users/action') }}">
    {{ csrf_field() }}
    ...
</form>

UserController massAction method that doesn't get reached:

public function massAction(Request $request)
{
    $userIds = $request->input('users');
    $user = new User();

    switch ($request->input('mass-action')) {
        case 1:
            $user->deleteUser($userIds);
            $request->session()->flash('message-success', 'User(s) deleted!');
            break;
    }

    return redirect()->back();
}

On form submit it should return back with a message, but it doesn't even reach the controller. Setting a breakpoint inside the method confirms that it doesn't reach it.

It just goes to /admin/users/action and returns 404 error, because this page doesn't exist. It should go to massAction method inside UserController and get redirected back to the page where form was submitted.

I am doing the same thing for products, attributes, etc. and it works fine. Only this route and method doesn't work. Other routes in the same UserController work.


Solution

  • The problem was in one of input fields. Have no idea why it didn't throw any errors or even reach the controller before.

    This

    <input class="entity-select" type="checkbox" name="specifications[{{ $user->id }}][id]" value="{{ $user->id }}">
    

    needed to be like this

    <input class="entity-select" type="checkbox" name="users[{{ $user->id }}[id]" value="{{ $user->id }}">
    

    After changing it back to the wrong input field name it now throws out a error and also reached breakpoint in UserController. Maybe some view cache problem? If that is a thing.