0

I am trying to setup a middleware to check if inputs are empty on form submit for updating a users settings, and if so to return them back to the same page with an error. When I set it up, it gives me the error

Too few arguments to function App\Http\Middleware\AdminUserUpdate::handle(), 2 passed in /var/www/market/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php on line 167 and exactly 3 expected

It seems to be the id I pass through, here is the rest of my code

Middleware:


public function handle(Request $request, Closure $next, $id)
    {
        if($request->input('username') == NULL) {
            return redirect()->route('admin.members.view', $id)->with('error', 'You must enter a username for this user in order to update their account!');
        } elseif($request->input('email') == NULL) {
            return redirect()->route('admin.members.view', $id)->with('error', 'You must enter a email for this user in order to update their account!');
        } elseif($request->input('admin') == NULL) {
            return redirect()->route('admin.members.view', $id)->with('error', 'You must select whether this user is a root admin or not in order to update their account!');
        } elseif($request->input('banned') == NULL) {
            return redirect()->route('admin.members.view', $id)->with('error', 'You must select whether this user is banned or not in order to update their account!');
        } elseif($request->input('role') == NULL) {
            return redirect()->route('admin.members.view', $id)->with('error', 'You must select a role for this user in order to update their account!');
        }

        return $next($request);
    }

Kernel:

'AdminUserUpdate' => \App\Http\Middleware\AdminUserUpdate::class,

Route:

Route::middleware(['AdminUserUpdate'])->group(function () {
        Route::post('/app/members/{id}/submit', 'App\Http\Controllers\Admin\Members\IndexController@Submit')->name('admin.members.submit'); 
    });

I have the ID passed through so I can return them back to the view page for the specific users id, but it doesnt seem to like that for some reason. Anyone have any thoughts?

3 Answers 3

1

Middlewares doesn't read route parameters. Use colon to pass a parameter to the middleware.

Route::middleware(['AdminUserUpdate:123'])->group(function () {

Link to Middleware Parameters docs

Sign up to request clarification or add additional context in comments.

3 Comments

How exactly would that work with my view page for each user ID?
Instead of using a middleware, add your validation/redirection at the start of your controller method.
Yeah that's what I usually do but wanted the controller function to be short and clean - guess that's not an option with how I am trying to do it. Thanks for the help!
1

I suggest to change the way you validate your fields.

In your case, this command create the form validator file

php artisan make:request UpdateUserRequest

In the file UpdateUserRequest.php

This block have all the rules you want to validate

public function rules()
{
    return [
        'username' => 'required',
        'email' => 'required',
        ... other fields for validation ...
    ];
}

and inside the store method in your Controller, only this line is required to manipulate all the data after the validation:

$validated = $request->validated();

and for custom messages, add this into your UpdateUserRequest:

public function messages()
{
    return [
        'username.required' => 'You must enter a username for this user in order to update their account!',
        'email.required' => 'You must enter a email for this user in order to update their account!',
        ... and other validation messages ...
    ];
}

For more details and ways to validate your forms, check the link:

Form Request Validation

Comments

0

I found a solution for this for Laravel 11 and 12 developers.
It is worth noting that the official Laravel 11 documentation has incomplete registration of the middleware (https://laravel.com/docs/11.x/middleware#middleware-parameters)

The complete registration of such middleware must also pass the variable in the middleware registration which is in this case:

'AdminUserUpdate' => \App\Http\Middleware\AdminUserUpdate::class,

shold be:

'AdminUserUpdate' => \App\Http\Middleware\AdminUserUpdate::class.':id',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.