0
如何在保存之前在型號上設置我自己的屬性,但僅當我創建新行/條目時?如何在保存模型Laravel 5.3+上設置默認的uuid?
示例PHP:
class foo extends controller {
// ..
public function create() {
$user = new User();
$user->name = 'Jack';
$user->lastname = 'Smith';
$user->save();
}
// ..
}
型號:
class User extends Model {
// ...
public function beforeSave() {
$this->token = uuid();
}
// ...
}
表:
CREATE TABLE User
(
`uuid` varchar(55),
`name` varchar(55) NOT NULL,
`lastname` varchar(55) NOT NULL,
primary key (`uuid`)
)
;
或許,這解決方案將幫助您或指示正確的方向:http://stackoverflow.com/questions/31817067/laravel-5-using-a -uuid-as-primary-key –