生成 Uuid 作为主键
Generating Uuid as Primary Key
拜托,我在生成 uuid 作为我的用户模型中的主键时遇到了问题。我总是 PHP 错误:Class 'App/Traits/boot' not found in C:/xampp/htdocs/twingle/app/Traits/UsesUuid.php on line 11. 尝试了各种方法但这个错误仍然存在
用户模型(App\User)
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Traits\UsesUuid;
class User extends Authenticatable
{
use Notifiable,UsesUuid;
protected $keyType = 'string';
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
使用一个特征
UseUuid(App\Traits)
<?php
namespace App\Traits;
use Ramsey\Uuid\Uuid;
trait UsesUuid
{
public static function UsesUuid()
{
boot::creating(function ($model) {
$model->setAttribute($model->getKeyName(), Uuid::uuid4());
});
}
}
用户迁移
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
如有任何帮助,我们将不胜感激。谢谢
你的特质代码看起来很不一致。它应该看起来像这样:
namespace App\Traits;
use Ramsey\Uuid\Uuid;
trait UsesUuid
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->setAttribute($model->getKeyName(), Uuid::uuid4());
});
}
}
这样当在模型中使用特征时,它会自动挂接到该模型的 creating
事件,并确保主键生成为 UUID
.
寻找模范观察员。它更干净,而且非常合身。 https://laravel.com/docs/5.8/eloquent#observers
拜托,我在生成 uuid 作为我的用户模型中的主键时遇到了问题。我总是 PHP 错误:Class 'App/Traits/boot' not found in C:/xampp/htdocs/twingle/app/Traits/UsesUuid.php on line 11. 尝试了各种方法但这个错误仍然存在
用户模型(App\User)
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Traits\UsesUuid;
class User extends Authenticatable
{
use Notifiable,UsesUuid;
protected $keyType = 'string';
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
使用一个特征
UseUuid(App\Traits)
<?php
namespace App\Traits;
use Ramsey\Uuid\Uuid;
trait UsesUuid
{
public static function UsesUuid()
{
boot::creating(function ($model) {
$model->setAttribute($model->getKeyName(), Uuid::uuid4());
});
}
}
用户迁移
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
如有任何帮助,我们将不胜感激。谢谢
你的特质代码看起来很不一致。它应该看起来像这样:
namespace App\Traits;
use Ramsey\Uuid\Uuid;
trait UsesUuid
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->setAttribute($model->getKeyName(), Uuid::uuid4());
});
}
}
这样当在模型中使用特征时,它会自动挂接到该模型的 creating
事件,并确保主键生成为 UUID
.
寻找模范观察员。它更干净,而且非常合身。 https://laravel.com/docs/5.8/eloquent#observers