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

DivDax's avatar
Level 10

Change Model Key Binding only for API Routes

Hey!

In my app i use the standard primary key (id) for all models. For all my API Routes i want to use uuid's for model key binding - but only for the api routes.

In my RouteServiceProvider i use this to "rebind" the models but the problem ist my models are using keyType = int so the binding is not working correctly. Any ideas?

public function boot()
{
    parent::boot();
    
    if(request()->is('api*')) {
        Route::model('users', function ($value) {
            return \App\User::whereUuid($value)->firstOrFail();
        });
    }
}
0 likes
1 reply
bobbybouwmann's avatar

Setting it to string should probably fix it! An integer is a string as well.

// app/User.php

class User extends Model
{
    protected $keyType = 'string';
}

Please or to participate in this conversation.