Created
February 12, 2019 07:46
-
-
Save DavidNgugi/29777291879cc6c326f9783a0a2ec1ac to your computer and use it in GitHub Desktop.
UUID trait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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