alexxa's avatar

Missing required parameters for [Route: admin.edit] [URI: admin/edit/{id}].

Hi! I have this error in Laravel 6. I am new to Laravel..I actually started a few days ago and I want to edit some user posts and I get this error. Here is my code form index.blade.php

@extends('layouts.admin')

@section('content')
    @if(Session::has('info'))
        <div class="row">
            <div class="col-md-12">
                <p class="alert alert-info">{{ Session::get('info') }}</p>
            </div>
        </div>
    @endif
    <div class="row">
        <div class="col-md-12">
            <a href="{{ route('admin.create') }}" class="btn btn-success">New Post</a>
        </div>
    </div>
    <hr>
    <div class="row">
        <div class="col-md-12">
            <p><strong>Learning Laravel</strong> <a href="{{ route('admin.edit', ['id' => 1]) }}">Edit</a></p>
        </div>
    </div>
@endsection

Here is my code from web.php

<?php

Route::get('/', function () {
    return view('blog.index');
})->name('blog.index');

Route::get('post/{id}', function ($id) {
    if($id==1){
        $post =[
            'title' => 'Learning Laravel',
            'content' => 'This blog post will get you right on track with Laravel'
        ];
    }else{
        $post =[
            'title' => 'Something else',
            'content' => 'Some other content'
        ];
    }
    return view('blog.post', ['post' => $post]);
})->name('blog.post');

Route::get('about', function () {
    return view('other.about');
})->name('other.about');

// here i grouped all the routes with the admin name
// so that if i want to change all the routes with the admin name
//i simple change the prefix, which won't ;ead to any errors

Route::group(['prefix' => 'admin'], function() {
    Route::get('', function () {
        return view('admin.index');
    })->name('admin.index');

    Route::get('create', function () {
        return view('admin.create');
    })->name('admin.create');
    
     //Dependency injection for post request
    Route::post('create', function(Illuminate\Http\Request $request) {
        return "It works!";
    })->name('admin.create');

    Route::get('edit/{id}', function ($id) {
        if($id==1){
            $post =[
                'title' => 'Learning Laravel',
                'content' => 'This blog post will get you right on track with Laravel'
            ];
        }else{
            $post =[
                'title' => 'Something else',
                'content' => 'Some other content'
            ];
        }
        return view('admin.edit', ['post' => $post]);
    })->name('admin.edit');
    //Dependency injection for post request
    Route::post('edit', function(Illuminate\Http\Request $request) {
        return redirect()
        ->route('admin.edit') 
        ->with ('info', 'Post edited. New title ' . $request -> input('title'));
    })->name('admin.update');
});

Thnak you ! :)

0 likes
14 replies
tisuchi's avatar

@alexxa

It seems fine to me.

Can you show us the full error message to get the idea in which file you are getting this error?

alexxa's avatar

I tried to clear it and I get an error:

 LogicException  : Unable to prepare route [api/user] for serialization. Uses Closure.

  at C:\laragon\www\laravelBasics\vendor\laravel\framework\src\Illuminate\Routing\Route.php:917
    913|      */
    914|     public function prepareForSerialization()
    915|     {
    916|         if ($this->action['uses'] instanceof Closure) {
  > 917|             throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
    918|         }
    919|
    920|         $this->compileRoute();
    921|

  Exception trace:

  1   Illuminate\Routing\Route::prepareForSerialization()
      C:\laragon\www\laravelBasics\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:62

  2   Illuminate\Foundation\Console\RouteCacheCommand::handle()
      C:\laragon\www\laravelBasics\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:32

The full error I get on the laravel app is:

Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameters for [Route: admin.edit] [URI: admin/edit/{id}].
Snapey's avatar

@usmanbasharmal Clear the route cache. Only use route cache on production servers otherwise you are having to run the command all the time.

By the way, the routes are not cacheable anyway, since they use closures so the command you gave will just show an error

Snapey's avatar

one issue

    Route::post('edit/{id}', function(Illuminate\Http\Request $request) {

when does the error appear? Just when you load the admin view, or when you click on the link?

Snapey's avatar

by the way, the error message shows other useful information such as what file the error occurred in

alexxa's avatar

Snapey, it appears when I try to edit a user post. I want to change the title of a post. I was following some Laravel 5 series as I like the way the tutor explains. However, I use Laravel 6 for my project as it is newer. I followed the steps exactly and I simply can't get what I am doing wrong.

Snapey's avatar

did you change the edit POST route as I illustrated?

alexxa's avatar

yes, and now I get this error:

Missing required parameters for [Route: admin.update] [URI: admin/edit/{id}]. (View: C:\laragon\www\laravelBasics\resources\views\admin\edit.blade.php)
Snapey's avatar

ok, so what does edit.blade.php look like?

You just need to fix the form action

1 like
alexxa's avatar

I solved the problem. I redirected it to the wrong route. I must have written it like this with admin.index:

 Route::post('edit', function(Illuminate\Http\Request $request)  {
        return redirect()
        ->route('admin.index') 
        ->with ('info', 'Post edited. New title ' . $request -> input('title'));
    })->name('admin.update');

Thank you very much for your patience !:) And hope I didn't mind you with my noob questions

UsmanBasharmal's avatar
Level 3

I think you have to do this

return view('admin.edit',compact('post','id');

Please or to participate in this conversation.