Skip to content

Commit 8fb4def

Browse files
committed
Add scope and find methods
1 parent bbf914e commit 8fb4def

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ class Project extends Model
2323
If your column name is not "uuid", simply add a new property to your model named "uuidFieldName":
2424

2525
protected $uuidFieldName = 'unique_id';
26+
27+
This trait also adds a scope:
28+
29+
```
30+
\App\Project::byUUID('uuid')->first();
31+
```
32+
33+
And static find method:
34+
35+
```
36+
\App\Project::findByUUID('uuid')
37+
```

src/Traits/HasUUID.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ public static function generateUUID(){
2424
return \Uuid::generate()->string;
2525
}
2626

27+
public function scopeByUUID($query, $uuid){
28+
return $query->where($this->getUUIDFieldName(),$uuid);
29+
}
30+
31+
public static function findByUuid($uuid){
32+
return static::byUUID($uuid)->first();
33+
}
34+
2735
}

0 commit comments

Comments
 (0)