Set UUID to User Table

How can I add uuid in user id table? I want to set generated user uuid but I can't see it's option in docs.
7 Replies
Matthew
Matthew16mo ago
Thats because its more of a laravel thing than a filament thing Create a new migration and add this:
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->uuid('uuid');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->uuid('uuid');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
User model:
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
use Filament\Panel;

class User extends Authenticatable implements FilamentUser
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'is_online',
'last_seen',
'uuid'
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

public function isAdmin(){
return $this->role === 'admin';
}

protected static function boot()
{
parent::boot();

static::creating(function ($model) {
$model->uuid = Str::uuid();
});
}
public function canAccessPanel(Panel $panel): bool
{
// return str_ends_with($this->email, '@gmail.com') && $this->hasVerifiedEmail();
return true;
}
}
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
use Filament\Panel;

class User extends Authenticatable implements FilamentUser
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'is_online',
'last_seen',
'uuid'
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

public function isAdmin(){
return $this->role === 'admin';
}

protected static function boot()
{
parent::boot();

static::creating(function ($model) {
$model->uuid = Str::uuid();
});
}
public function canAccessPanel(Panel $panel): bool
{
// return str_ends_with($this->email, '@gmail.com') && $this->hasVerifiedEmail();
return true;
}
}
Did this help? @mgkyawzayya
mgkyawzayya
mgkyawzayyaOP16mo ago
Thanks 🙏 I forget the Model boot method
Matthew
Matthew16mo ago
Oke no problem!
Matthew
Matthew16mo ago
Also, please mark the solution as such 😅
Matthew
Matthew16mo ago
Otherwise it isnt shown in the threads xd
mgkyawzayya
mgkyawzayyaOP16mo ago
Done ✅
Matthew
Matthew16mo ago
Oh oke sorry, didnt see it Thanks
Want results from more Discord servers?
Add your server