rits
rits

Reputation: 1544

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.

Upvotes: 2

Views: 4926

Answers (4)

Ashwani Garg
Ashwani Garg

Reputation: 1527

Use put in the form after CSRF like this.

@csrf
	@if(isset($category))
		@method('PUT')
	@endif

Upvotes: 0

rits
rits

Reputation: 1544

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.

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21691

I think you need change your route like:

web.php route:

 Route::post('/admin/users/action','UserController@massAction')->name('user.massaction');

form:

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

Upvotes: 1

Patryk Woziński
Patryk Woziński

Reputation: 746

Try to use route() instead url().

Change action in from url() to route() and check this out.

action="{{ route('user.massAction') }}"

Upvotes: 2

Related Questions