skip to Main Content

Trying to submit the form using POST method and also getting the parameters via url. While accessing website.com/profile/ it doesn’t work. It returns this error message

The GET method is not supported for route profile-list. Supported
methods: POST.

I’m not sure what I’m doing wrong

Web.php

Route::get('/profile/{username}/{mobilenumber?}', [ProfileController::class, 'profileList'])->name('profile-list');
Route::post('/profile/', [ProfileController::class, 'submitForm'])->name('submit-form');

Blade form file

<form action="{{ route('submit-form') }}" method="post">
   @csrf
   ....
</form>

ProfileController

public function submitForm(Request $request) 
    {
        $request->validate(
            ...
        );

        $username = strtolower($request->input('username'));
        $mobilenumber = strtolower($request->input('mobilenumber'));

        if ($mobilenumber) {
            return redirect()->route('profile-list', ['username' => $username, 'mobilenumber' => $mobilenumber]);
        } else {
            return redirect()->route('profile-list', ['username' => $username]);
        }
    }

2

Answers


  1. Try to change your route definition like this

    Route::post('/profile', 'ProfileController@submitForm')->name('submit-form');
    

    and also add => @method(‘POST’)

    <form action="/profile" method="post">
        @method('POST')
        @csrf
    </form>
    
    Login or Signup to reply.
  2. First of all, you need to add the params on the route you will use

    Route::post('/profile/{username}/{mobileNumber?}', [ProfileController::class, 'submitForm'])->name('submit-form');
    

    This is because you are using the params in ‘submitForm’ method. This
    have not relation with the another method.

    Your form

    <form action="{{ route('submit-form', ['username' => $username, 'mobileNumber' => $mobileNumber]) }}" method="post">
       @csrf
       ....
    </form>
    

    This will add the params to the route.

    And finally, your handler method [PLEASE SEE THE ANOTHER IMPLEMENTATION]

    public function submitForm(Request $request, $username, $mobileNumber = null) 
        {
            $request->validate(
                ...
            );
    
            $username = strtolower($username);
            // I think you dont need strtolower for the phone number
    
            if ($mobilenumber != null) {
                return redirect()->route('profile-list', ['username' => $username, 'mobilenumber' => $mobilenumber]);
            } else {
                return redirect()->route('profile-list', ['username' => $username]);
            }
        }
    

    I tested after write this, and route helper works fine with null values, so a better approach is as following:

    public function submitForm(Request $request, $username, $mobileNumber = null) 
            {
                $request->validate(
                    ...
                );
    
            $username = strtolower($username);
            // I think you dont need strtolower for the phone number
    
            return redirect()->route('profile-list', ['username' => $username, 'mobilenumber' => $mobilenumber]);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search