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

Laravel Confusion with POST Routes for Ticket Comments and Updates

I have two POST routes in my Laravel application: one for commenting on a ticket and another for updating a ticket. However, I think Laravel is getting confused about which route to pick. I’m trying to understand why this confusion is occurring, and consequently, I’m a bit confused as well.

Here are the routes:

Route::post('/tickets/{ticket}', 'update')->name('tickets.update');
// <form action="{{ route('tickets.update', ['ticket' => $ticket->TicketId]) }}" method="POST">
Route::post('/tickets/{id}', [CommentController::class, 'store'])->name('comments.store');
// <form method="POST" action="{{ route('comments.store', ['id' => $ticket->TicketId]) }}">

I don’t think it’s that hard to understand that both routes are using IDs, which is likely why there’s confusion. I thought that naming the routes and using different slugs would help prevent this issue.

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

Now, while it wouldn’t be a big deal to just change the POST route name for comments to something like /comments/{id}, I’m still curious: how can this confusion happen even when I’m naming the routes and using different slugs?

Thanks in advance for your help!

What i’ve tried: I checked if the routes were being accessed, which they weren’t. Then I tried naming them differently, which worked, but it still left me confused, hence the question.

>Solution :

Both of your route definitions are using dynamic parameter ({ticket} and {id}) with the same HTTP and they both start with /tickets. Laravel matches routes sequentially and attempts to match the URL to the first pattern that fits. Since both routes follow the same pattern Laravel likely matches the first route, thinking the second route should also be handled by it.

Laravel takes a "last in, first out" approach, where any previous definition of the same route is overwritten. In your case, even though you’ve named the routes differently and use distinct controllers, the route patterns are still the same, leading to ambiguity.

So, even if you call the route using the name helper, Laravel will look in to the routes file, find the latest URL that matches the pattern, and overwrite the first definition of route. You can confirm this by running php artisan route:list in the command line to see what route definitions Laravel will actually attempt to match.

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