-
Description:Hi folks, I use UUIDs across all my Laravel models, and it in general works as expected, except for the I'll give the below example using the Apparently, it doesn't understand that I'm using UUID instead of the numeric ID, so the 'notification_id' field is incorrectly generated as integer instead of UUID. Detailed steps to reproduce the issue on a fresh Nova installation:On a fresh Nova installation, change your user migration to use UUID instead of ID. <?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
...Create a trait to handle UUIDs in your models. <?php
declare(strict_types=1);
namespace App\Traits;
use Illuminate\Support\Str;
trait Uuid
{
/**
* Boot function from Laravel.
*
* @return void
*/
protected static function boot(): void
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = Str::uuid()->toString();
}
});
}
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing(): bool
{
return false;
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType(): string
{
return 'string';
}
}Then import it to your User model. <?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, Uuid;
....To be able to test your application, run the command ResultsAs can be seen, the user is created as expected. The error can be seen when you log into your Nova application: Nova is querying a string value in a numeric field. Up to this point, I thought that for any reason, the migrations were not understanding that I was using the UUID trait, and decided to set the I have even set the Going deeperChecking the file I see that behind the scenes, the As a workaround, I've copied this migration to my database/migrations folder and I changed it to use I'm wondering if this is a bug, or if I'm missing something here, maybe something I didn't see in the documentation. Thanks in advance for the help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
If all models in your application use UUID (string) as the primary key then you should be able to run the following: $this->app['db.schema']->morphUsingUuids();Typically from |
Beta Was this translation helpful? Give feedback.


If all models in your application use UUID (string) as the primary key then you should be able to run the following:
Typically from
App\Providers\AppServiceProvider