Tôi có bảng mysql trong đó tôi đang sử dụng UUID làm khóa chính. Dưới đây là cuộc di cư tạo:Khóa chính UUID trong mô hình Eloquent được lưu dưới dạng uuid nhưng trả về là 0
Schema::create('people', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
...
$table->timestamps();
}
nào tạo ra schema MySQL sau:
CREATE TABLE `people` (
`id` char(36) COLLATE utf8_unicode_ci NOT NULL,
...
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Trong mô hình hùng biện của tôi, tôi có một phương pháp để tạo một thể hiện trong đó kêu gọi một phương pháp để tạo ra các UUID:
class Person extends Model
{
protected $fillable = [
...
];
public function make(array $personData){
$person = new Person;
$person->setUUID();
collect($personData)->each(function ($value, $columnName) use($person){
if(in_array($columnName, $this->fillable)){
$person->{$columnName} = $value;
}
});
$person->save();
return $person;
}
protected function setUUID(){
$this->id = preg_replace('/\./', '', uniqid('bpm', true));
}
}
Khi tôi tạo một phiên bản mẫu mới, nó lưu trữ nó trong cơ sở dữ liệu:
Nhưng khi tôi cố gắng để truy cập id dụ mới:
Nó trả về là 0:
tôi thiếu gì ở đây?