Skip to content
Advertisement

How to use UUID instead of a slug for ticket

I am currently navigating to a ticket using the slug set by the title example blah-blah-blah and want to use uuid instead of 336c64de-5dcb-34bc-9511-a48242b9zzzb. What is the best approach to take for Laravel 8? I am using Laravel Jetstream with Livewire stack.

Current model code

public function getRouteKeyName()
{
    return 'slug';
}

public function setSlugAttribute($slug)
{
    $this->attributes['slug'] = Str::slug($slug);
}

public function path()
{
    return '/tickets/' . $this->slug;
}

Ticket Migration

Schema::create('tickets', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id');
    $table->foreignId('ticket_type_id');
    $table->string('slug')->unique();
    $table->string('title');
    $table->text('body');
    $table->string('email')->nullable();
    $table->string('url')->nullable();
    $table->boolean('locked')->default(false);
    $table->unsignedInteger('visits')->default(0);
    $table->timestamps();
});

Advertisement

Answer

add uuid in migration

$table->uuid('uuid')->unique();

if you want to use uuid for foreign key you have to add

$table->uuid('ticket_type_id');
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement