Skip to content

Commit

Permalink
FIX: Removes column ambiguity and auto disables incrementing
Browse files Browse the repository at this point in the history
Removes column ambiguity when using named scopes, an issue that would occur if you are using joins in your query on a table that also contains the uuid field.

```php
$query->where(function ($query) {
    $query->join('tableB', 'tableA.columnA', '=', 'tableB
})->uuid($uuid);
```

Also auto-disables incrementing if the uuid column is set to `id`, which is useful in that developers no longer need to add this property to potentially dozens of Models.
  • Loading branch information
zanderwar authored May 15, 2022
1 parent 2b7456e commit 1f5a6fb
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 1f5a6fb

Please sign in to comment.