Did the one that worked also extend User model?
UUID column is getting NULL on create
Hello guys, I'm having this bizarre problem in my Laravel application. I was migrating all my models to use UUIDs instead of incrementing IDs. I was able to do so for one model without issues. But when I wanted to do the same for the second model, all my tests are blowing up complaining about a NULL value for the ID.
create_clients_table.php
Schema::create('clients', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->timestamps();
// more fields
});
Client.php
use Illuminate\Foundation\Auth\User;
class Client extends User
{
use HasFactory, HasApiTokens, Notifiable;
protected $fillable = [
// fillable fields
];
public $incrementing = false;
protected $keyType = 'string';
protected $casts = [
'id' => 'string'
];
protected static function boot()
{
parent::boot();
static::creating(function (Model $model) {
$model->setAttribute($model->getKeyName(), Uuid::uuid4());
});
}
//
}
ClientController.php
public function store()
{
$client = Client::where('phone_number', request()->phone_number)->first();
if (!$client) {
$client = Client::create([
// fillable fields
]);
return response()->json($client, 201);
} else {
// attempt login
}
}
The weird thing about this issue is the fact that everything seems to be working fine when I try to create a Client in php artisan tinker. But the tests that back the client creation process and stuff are all failing. What am I missing here? Anything I should check? I'm really lost here.
Please or to participate in this conversation.