gitHub地址: https://github.com/EmadAdly/laravel-uuid.git
1、安裝依賴
composer require emadadly/laravel-uuid
2、然后在config/app.php的providers里添加ServiceProvider
'providers' => [
...
Emadadly\LaravelUuid\LaravelUuidServiceProvider::class,
],
3、然后根目錄執(zhí)行
php artisan vendor:publish --provider="Emadadly\LaravelUuid\LaravelUuidServiceProvider"
執(zhí)行完的效果是:在config下生成uuid.php
4、使用
(1)主鍵id不使用uuid,新建一行儲存uuid的列
在config/uuid.php
'default_uuid_column' => 'uuid',
(2) 主鍵id就直接使用uuid
在config/uuid.php中將uuid改成id
'default_uuid_column' => 'id',
在migration中建表時:
Schema::create('sys_user', function (Blueprint $table) {
$table->uuid('id')->unique();
....
$table->timestamps();
});
在實體類如User.php中使用uuid,加入
use Uuids;
....
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
然后在Controller中在新增數(shù)據(jù)時id默認可以直接使用uuid
For Example:
public function store(Request $request)
{
$data = $request->json()->all();
$Article=Article::create($data);
return response()->json($Article);
}
修改自帶的created_at和updated_at
const UPDATED_AT='update_date';
const CREATED_AT = 'create_date';
以上這篇Laravel自動生成UUID,從建表到使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法
- laravel批量生成假數(shù)據(jù)的方法
- Laravel 自動生成驗證的實例講解:login / logout
- Laravel 自定命令以及生成文件的例子