From 70cd8328768672e8eaf9e882503fcf286b0ac944 Mon Sep 17 00:00:00 2001 From: Jake Mitchell Date: Sun, 26 Jun 2016 01:00:13 +0000 Subject: [PATCH] added uuid query scope --- README.md | 14 ++++++++++++++ src/Contracts/MetaContract.php | 9 +++++++++ src/Meta.php | 11 +++++++++++ tests/MetaTest.php | 11 +++++++++++ 4 files changed, 45 insertions(+) diff --git a/README.md b/README.md index f30a5f0..04f7bab 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,20 @@ And to retrieve meta information, use the 'read' method. $post->read('author'); //returns 'Bob Saget' ``` +### Query Builder + +Let's say you need to retrieve the post that has a certain UUID (maybe you gave it out in a url or something) + +It's super easy to search for it: +``` php +Post::withUuid($uuid)->first(); +``` + +It is completely integrated into the query builder, so you can still do stuff like: +``` php +Post::withUuid($uuid)->pluck('username'); +Post::withUuid($uuid)->firstOrFail(); +``` ## Credits diff --git a/src/Contracts/MetaContract.php b/src/Contracts/MetaContract.php index 47bc3df..a438266 100644 --- a/src/Contracts/MetaContract.php +++ b/src/Contracts/MetaContract.php @@ -77,4 +77,13 @@ interface MetaContract * @return void */ public function setRawUuid( \Ramsey\Uuid\Uuid $uuid ); + + /** + * Get the model that has this UUID. + * + * @param string $uuid + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public static function withUuid( $uuid ); } \ No newline at end of file diff --git a/src/Meta.php b/src/Meta.php index 5f2e06f..90756aa 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -126,6 +126,17 @@ class Meta extends Model implements FoundationModel, MetaContract $this->uuid = $uuid; $this->save(); } + + /** + * Get the model that has this UUID. + * + * @param string $uuid + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public static function withUuid( $uuid ){ + return parent::where('uuid', $uuid); + } /** * Add the UUID and Meta columns to the table. diff --git a/tests/MetaTest.php b/tests/MetaTest.php index 1e67d58..5e26633 100644 --- a/tests/MetaTest.php +++ b/tests/MetaTest.php @@ -30,4 +30,15 @@ class MetaTest extends \PHPUnit_Framework_TestCase $model->setRawUuid($uuid); $this->assertEquals($uuid, $model->getUuid()); } + + public function testUuidQuery () + { + $model = Meta::create([ + 'meta' => serialize(['name' => 'Tony']) + ]); + + $queried = Meta::withUuid($model->getUuid())->first(); + + $this->assertEquals($model->read('name'), $queried->read('name')); + } }