sami
sami

Reputation: 71

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');

Upvotes: 2

Views: 19407

Answers (3)

sradha
sradha

Reputation: 2244

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.

Upvotes: 1

Aless55
Aless55

Reputation: 2709

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]);

Upvotes: 6

akash_poojary
akash_poojary

Reputation: 53

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

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

Upvotes: 2

Related Questions