'Missing required parameters for Route (Laravel 8)

I get this error when I create or edit a form, do you know if it comes from one of these views or from the Controller?

Missing required parameter for [Route: forms.show] [URI: forms/{form}] [Missing parameter: form]

Thankstrong texts for your help, this is my first post here.

Code below :

CONTROLLER FormsController:

public function create()
    {
        if (!Auth::check()) {
            return redirect('login');
        }
        return view('forms.create');
    }

public function show($id)
    {
        return view('forms.consult', ['forms' => Forms::findOrFail($id)]);
    }

public function update(StoreFormsRequest $request, Forms $forms)
    {
        if (!Auth::check()) {
            return redirect('login');
        }
        $request->validated();
        $forms->update($request->input());
        return redirect()->route('forms.show', ['forms' => $forms]);
    }

public function edit($id)
    {
        if (!Auth::check()) {
            return redirect('login');
        }
        return view('forms.edit', ['forms' => Forms::findOrFail($id)]);
    }

VIEW create.blade.php:

<form action="{{ url('forms') }}" method="POST">
  @csrf
...
<button class="btn btn-primary mb-1 mr-1" type="submit"> Ajouter </button>
</form>

edit.blade.php :

<form action="{{ url('forms', [$forms->id]) }}" method="POST">
  @csrf
  @method('PUT')
...
<button class="btn btn-primary mb-1 mr-1" type="submit"> Modifier </button>
</form>

ROUTE

web.php :
Route::get('/', [FormsController::class, 'index']);
Route::resource('forms', FormsController::class);

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/home', [App\Http\Controllers\FormsController::class, 'index'])->name('home');


Solution 1:[1]

The problem is that your controller passes a parameter called forms instead of form. But your route is expecting a form parameter.

Change this:

return redirect()->route('forms.show', ['forms' => $forms]);

to this:

return redirect()->route('forms.show', ['form' => $forms]);

Solution 2:[2]

Make sure you are passing 'form' parameter to the route. Use helper methods in view for example as

{{ route('forms.show', ['form' => 1]) }}

Solution 3:[3]

By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name.

you need to pass the singular name of the resource ie singular name of the controller FormsController should be pass as form instead of forms.

https://laravel.com/docs/8.x/controllers#restful-naming-resource-route-parameters

I faced the same issue in my project , i passed singular name of controller and it worked.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 akash_poojary
Solution 3 sradha