Skip to content

Instantly share code, notes, and snippets.

@DavidNgugi
Created February 12, 2019 07:46
Show Gist options
  • Save DavidNgugi/29777291879cc6c326f9783a0a2ec1ac to your computer and use it in GitHub Desktop.
Save DavidNgugi/29777291879cc6c326f9783a0a2ec1ac to your computer and use it in GitHub Desktop.
UUID trait
<?php
namespace App\Helpers\Traits;
use Illuminate\Support\Str;
trait UuidTrait
{
public function scopeByUuid($query, $uuid)
{
return $query->where('uuid', $uuid);
}
public static function bootUuidScopeTrait()
{
static::creating(function($model){
if (empty($model->uuid)) {
$model->uuid = (string) Str::uuid();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment