Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 2696273

Browse files
committed
Uuid Observer
1 parent 2daaad3 commit 2696273

File tree

6 files changed

+114
-2
lines changed

6 files changed

+114
-2
lines changed

app/Entities/Users/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
3434
*
3535
* @var array
3636
*/
37-
protected $fillable = ['name', 'email', 'password'];
37+
protected $fillable = ['uuid', 'name', 'email', 'password'];
3838

3939
/**
4040
* The attributes excluded from the model's JSON form.

app/Observers/UuidObserver.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Observers;
4+
5+
use Webpatser\Uuid\Uuid;
6+
7+
/**
8+
* Class UuidObserver
9+
* @package App\Observers
10+
*/
11+
class UuidObserver
12+
{
13+
14+
/**
15+
* @param $model
16+
* @throws \Exception
17+
*/
18+
public function creating($model)
19+
{
20+
$model->uuid = Uuid::generate(4);
21+
}
22+
23+
}

app/Providers/EventServiceProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Providers;
44

5+
use App\Entities\Users\User;
6+
use App\Observers\UuidObserver;
57
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
68
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
79

@@ -25,7 +27,15 @@ class EventServiceProvider extends ServiceProvider
2527
public function boot(DispatcherContract $events)
2628
{
2729
parent::boot($events);
30+
$this->registerObservers();
31+
}
2832

29-
//
33+
/**
34+
* @return $this
35+
*/
36+
public function registerObservers()
37+
{
38+
User::observe(app(UuidObserver::class));
39+
return $this;
3040
}
3141
}

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function up()
1414
{
1515
Schema::create('users', function (Blueprint $table) {
1616
$table->increments('id');
17+
$table->string('uuid')->index();
1718
$table->string('name');
1819
$table->string('email')->unique();
1920
$table->string('password', 60);

readme.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,58 @@ $api->version('v1', function ($api) {
167167
);
168168
```
169169

170+
## Uuid
171+
172+
The started kit comes with a UUID observer to add to your models, this way the uuid will be generated when the model is being created.
173+
174+
This is an example for the User model.
175+
176+
```php
177+
namespace App\Providers;
178+
179+
use App\Entities\Users\User;
180+
use App\Observers\UuidObserver;
181+
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
182+
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
183+
184+
/**
185+
* Class EventServiceProvider
186+
* @package App\Providers
187+
*/
188+
class EventServiceProvider extends ServiceProvider
189+
{
190+
/**
191+
* The event listener mappings for the application.
192+
*
193+
* @var array
194+
*/
195+
protected $listen = [
196+
197+
];
198+
199+
/**
200+
* Register any other events for your application.
201+
*
202+
* @param \Illuminate\Contracts\Events\Dispatcher $events
203+
* @return void
204+
*/
205+
public function boot(DispatcherContract $events)
206+
{
207+
parent::boot($events);
208+
$this->registerObservers();
209+
}
210+
211+
/**
212+
* @return $this
213+
*/
214+
public function registerObservers()
215+
{
216+
User::observe(app(UuidObserver::class));
217+
return $this;
218+
}
219+
}
220+
```
221+
170222
## Tests
171223

172224
Navigate to the project root and run `vendor/bin/phpunit` after installing all the composer dependencies and after the .env file was created.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Tests\Observers;
4+
5+
use App\Tests\TestCase;
6+
use App\Entities\Users\User;
7+
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
9+
class UuidObserverTest extends TestCase
10+
{
11+
12+
use DatabaseMigrations;
13+
14+
/**
15+
* @test
16+
*/
17+
public function itGeneratesUuidForUser()
18+
{
19+
$user = factory(User::class)->create();
20+
$this->assertFalse(empty($user->uuid));
21+
$this->seeInDatabase('users', [
22+
'uuid' => $user->uuid->string
23+
]);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)