Uuid.php 1.33 KB
<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait Uuid
{
    /*
    |----------------------------------------------------------------------------------------------
    | perlu definisikan alias jika di model ada method "boot" juga, contoh alias didalam class:
    |----------------------------------------------------------------------------------------------
    |   class Nama extends Model
    |   {
    |       use SoftDeletes;
    |       use Uuid {
    |           boot as uuidBoot;
    |       }
    |   }
    |----------------------------------------------------------------------------------------------
    | dan panggil dalam boot model, contoh pemanggilan :
    |----------------------------------------------------------------------------------------------
    |   public static function boot()
    |   {
    |       parent::boot();
    |       static::uuidBoot();
    |   }
    |----------------------------------------------------------------------------------------------
    */
    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            try {
                $model->id = $model->id == '' ? (string) Str::Uuid():$model->id;
            } catch (UnsatisfiedDependencyException $e) {
                abort(500, $e->getMessage());
            }
        });
    }
}