From 83b4f371487860ca07dc8a0c9eb3c9174b3afad1 Mon Sep 17 00:00:00 2001 From: f3brysan <febrysan1995@gmail.com> Date: Tue, 28 Mar 2023 16:03:35 +0700 Subject: [PATCH] #7: Auto Generate UUID --- app/Traits/Uuid.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/Traits/Uuid.php diff --git a/app/Traits/Uuid.php b/app/Traits/Uuid.php new file mode 100644 index 0000000..b7b701f --- /dev/null +++ b/app/Traits/Uuid.php @@ -0,0 +1,22 @@ +<?php +namespace App\Traits; + +use Illuminate\Support\Str; +use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; + +trait Uuid +{ + protected static function boot() + { + parent::boot(); + + static::creating(function ($model) { + try { + $model->id = (string) Str::uuid(); // generate uuid + // Change id with your primary key + } catch (UnsatisfiedDependencyException $e) { + abort(500, $e->getMessage()); + } + }); + } +} \ No newline at end of file -- libgit2 0.26.0