Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why are my Laravel route model bindings returning a 404 error?

I’m working on a Laravel project and using route model binding to automatically inject model instances into my routes. However, I’m encountering an issue where some routes return a 404 error even though the model
instance exists in the database.

I have defined a route in my web.php file like this:

Route::get('/posts/archived/{post}', [PostController::class, 'show']);
PostController
namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }
}

Despite having records in the posts table, when I navigate to /posts/1 or any other valid ID, I get a 404 error. I’ve verified that the records exist in the database

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Check your Route. It is incorrect. You should hit this route: /posts/archived/1

OR

you should change your original route to: /posts/{post} in web.php

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading