added uuid query scope

master
Jake Mitchell 8 years ago
parent ddea4dd573
commit 70cd832876

@ -102,6 +102,20 @@ And to retrieve meta information, use the 'read' method.
$post->read('author'); //returns 'Bob Saget' $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 ## Credits

@ -77,4 +77,13 @@ interface MetaContract
* @return void * @return void
*/ */
public function setRawUuid( \Ramsey\Uuid\Uuid $uuid ); 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 );
} }

@ -126,6 +126,17 @@ class Meta extends Model implements FoundationModel, MetaContract
$this->uuid = $uuid; $this->uuid = $uuid;
$this->save(); $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. * Add the UUID and Meta columns to the table.

@ -30,4 +30,15 @@ class MetaTest extends \PHPUnit_Framework_TestCase
$model->setRawUuid($uuid); $model->setRawUuid($uuid);
$this->assertEquals($uuid, $model->getUuid()); $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'));
}
} }

Loading…
Cancel
Save