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;
$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);