Skip to content

Commit

Permalink
Merge pull request #43 from zanderwar/patch-1
Browse files Browse the repository at this point in the history
FIX: Removes column ambiguity and auto disables incrementing
  • Loading branch information
EmadAdly authored Jul 17, 2023
2 parents 3960687 + 1f5a6fb commit 3f9bb62
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Uuids.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Ramsey\Uuid\Uuid;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;

trait Uuids
{
Expand All @@ -12,7 +13,10 @@ trait Uuids
*/
protected static function bootUuids()
{
static::creating(function ($model) {
static::creating(function (Model $model) {
if (config('uuid.default_uuid_column') === 'id') {
$model->incrementing = false;
}
if (!$model->{config('uuid.default_uuid_column')}) {
$model->{config('uuid.default_uuid_column')} = Uuid::uuid4()->toString();
}
Expand All @@ -39,7 +43,7 @@ public function scopeUuid($query, $uuid, $first = true)
throw (new ModelNotFoundException)->setModel(get_class($this));
}

$results = $query->where(config('uuid.default_uuid_column'), $uuid);
$results = $query->where($this->getTable().'.'.config('uuid.default_uuid_column'), $uuid);

return $first ? $results->firstOrFail() : $results;
}
Expand Down

0 comments on commit 3f9bb62

Please sign in to comment.