Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zwies01's avatar

Laravel 404 on new routes with parameters

Whenever I try to create a new route with a parameter it gives me a 404 error. However, when I delete the parameters, it all works just fine. I tried all the artisan commands:

  • route:clear
  • cache:clear
  • config:cache
  • composer dump-autoload

When I check route:list, the routes are also there. At this point, I'm starting to pull my hair out. I just don't know what's going on here.

I'm currently running on Laravel 8.54.

(And I have also restarted my Mac).

0 likes
7 replies
click's avatar

Can you give us an example of the route you defined and the route you are trying to visit?

If you define a route with a parameter Laravel might start searching your database for a model to match that parameter. This is called route model binding https://laravel.com/docs/8.x/routing#route-model-binding. If the model is not found it throws a ModelNotFoundException which shows a 404 on your screen.

1 like
zwies01's avatar

Sure!

Route::get('/invoices/edit/{invoice}', [App\Http\Controllers\InvoiceController::class, 'edit'])->name('invoices.edit');

public function edit(Invoice $invoice) { var_dump($invoice); die(); }

And the full url to access it: [domain].test/invoices/edit/1001

"If the model is not found it throws a ModelNotFoundException which shows a 404 on your screen."

The index, create, show and store do work without any issue.

click's avatar

@zwies01

The index, create, show and store do work without any issue.

That is because those routes do not contain a parameter.

And the full url to access it: [domain].test/invoices/edit/1001

Do you have an invoice with id 1001 ? If so, did you change anything regarding Route Model Binding?

Can you somewhere manually run: dd((new \App\Models\Invoice())->resolveRouteBinding(1001));, do you get a result?

1 like
zwies01's avatar

@click I do have an invoice with id 1001, but when I try

dd((new \App\Models\Invoice())->resolveRouteBinding(1001));

It gives me a null

zwies01's avatar

@Sinnbeck I made a mistake in my GetRouteKeyName function. Where It should have returned "invoice_id", it returned "invoice_id ". After removing the extra space at the end, the issue was resolved.

zwies01's avatar
zwies01
OP
Best Answer
Level 1

After checking my model I see that my function "getRouteKeyName()" has an extra added space in the return value. I'm crying rn lol. Issue is resolved now! Thank you for your help!

public function getRouteKeyName() {
    return 'invoice_id';
}

Please or to participate in this conversation.