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

Using laravels built in UUID doesnt work with route model binding

I am using Laravel 10 … I have setup my project to use Laravel’s inbuilt UUID.

In my post model im using the use HasUuids; trait and importing use Illuminate\Database\Eloquent\Concerns\HasUuids;

In my migration im setting it up as follows;

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

$table->uuid('id')->primary();

This seems to all be going to plan, posts get saved with a UUID etc etc .. The only problem is when i am trying to use Route Model Binding … It just doesnt work.

I have an endpoint like so;

Route::get('post/{post:uuid}', PostController::class);

and in the controller definition i use;

public function __invoke(Request $request, Post $post)

But it just doesnt work, nothing i do can get it working.

Has anybody else seen this? I would love some help getting this going.

>Solution :

The issue with your route definition is that you have used "uuid" as the custom slug for model route binding, but the primary key of your "posts" table is actually "id". Therefore, you should update your route definition to use "id" instead of "uuid".
The correct route definition would be

Route::get('post/{post}', PostController::class);
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