Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search still broken in latest Nova with UUID #8

Open
LiamKarlMitchell opened this issue Aug 5, 2022 · 1 comment
Open

Search still broken in latest Nova with UUID #8

LiamKarlMitchell opened this issue Aug 5, 2022 · 1 comment

Comments

@LiamKarlMitchell
Copy link

Also if I type a single character such as f it will error something to do with an aggregate search.

If I edit the Search/PrimaryKey 's invoke method to put a cast it works.
But how to apply such a modification to Nova?

$cast = '';
if ($connectionType === 'pgsql' && $model->getKeyType() === 'uuid') {
    $cast = '::text';
}
return $query->{$whereOperator}($model->getQualifiedKeyName().$cast, $search);
@LucWollants
Copy link
Member

There is another approach to make Laravel Nova work with models that have a UUID as their primary key. This approach requires two changes:

  • Add the method Schema::morphUsingUuids() to the boot method of NovaServiceProvider. This will force the migration to create an action table with string types instead of int types
  • Create a trait or abstract class for your custom models that set the primary key type to string and sets a UUID value when the primary key is empty when creating a new model.
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;

abstract class UuidModel extends Model
{
    protected static function boot(): void
    {
        parent::boot();

        static::creating(static function ($model) {
            if (empty($model->{$model->getKeyName()})) {
                $model->{$model->getKeyName()} = Uuid::uuid4()->toString();
            }
        });
    }

    public function getIncrementing(): bool
    {
        return false;
    }

    public function getKeyType(): string
    {
        return 'string';
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants