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
Thats because its more of a laravel thing than a filament thing
Create a new migration and add this:
User model:
Did this help? @mgkyawzayya
<?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) {
//
});
}
};
<?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;
}
}
Thanks 🙏 I forget the Model boot method
Oke no problem!
Also, please mark the solution as such 😅
Otherwise it isnt shown in the threads xd
Done ✅
Oh oke sorry, didnt see it
Thanks