Skip to content

Commit 747240a

Browse files
authored
Add HasUuids trait when model uses uuid primary key (#655)
1 parent 6d1bcf6 commit 747240a

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

src/Generators/ModelGenerator.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,21 @@ protected function buildRelationships(Model $model): string
271271

272272
protected function addTraits(Model $model, $stub): string
273273
{
274-
if (!$model->usesSoftDeletes()) {
275-
return $stub;
274+
$traits = ['HasFactory'];
275+
276+
if ($model->usesSoftDeletes()) {
277+
$this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes');
278+
$traits[] = 'SoftDeletes';
279+
}
280+
281+
if ($model->usesUuids()) {
282+
$this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids');
283+
$traits[] = 'HasUuids';
276284
}
277285

278-
$this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes');
279-
$stub = Str::replaceFirst('use HasFactory', 'use HasFactory, SoftDeletes', $stub);
286+
sort($traits);
280287

281-
return $stub;
288+
return Str::replaceFirst('use HasFactory', 'use ' . implode(', ', $traits), $stub);
282289
}
283290

284291
private function fillableColumns(array $columns): array

src/Models/Model.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function usesPrimaryKey(): bool
9696
return $this->primaryKey !== false;
9797
}
9898

99+
public function usesUuids(): bool
100+
{
101+
return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid';
102+
}
103+
99104
public function disablePrimaryKey(): void
100105
{
101106
$this->primaryKey = false;

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,38 @@ public function output_generates_models_with_custom_pivot(): void
595595
$this->assertEquals(['created' => ['app/Models/User.php', 'app/Models/Team.php', 'app/Models/Membership.php']], $this->subject->output($tree));
596596
}
597597

598+
#[Test]
599+
public function output_generates_models_with_hasuuids_trait_if_uuid_id_is_type_uuid(): void
600+
{
601+
$this->filesystem->expects('stub')
602+
->with('model.class.stub')
603+
->andReturn($this->stub('model.class.stub'));
604+
$this->filesystem->expects('stub')
605+
->times(1)
606+
->with('model.casts.stub')
607+
->andReturn($this->stub('model.casts.stub'));
608+
$this->filesystem->expects('stub')
609+
->times(1)
610+
->with('model.fillable.stub')
611+
->andReturn($this->stub('model.fillable.stub'));
612+
$this->filesystem->expects('stub')
613+
->times(1)
614+
->with('model.method.stub')
615+
->andReturn($this->stub('model.method.stub'));
616+
617+
$this->filesystem->expects('exists')
618+
->times(1)
619+
->with('app/Models')
620+
->andReturnTrue();
621+
622+
$this->filesystem->expects('put')
623+
->with('app/Models/User.php', $this->fixture('models/model-with-uuid-trait.php'));
624+
$tokens = $this->blueprint->parse($this->fixture('drafts/model-with-uuid-id.yaml'));
625+
$tree = $this->blueprint->analyze($tokens);
626+
627+
$this->assertEquals(['created' => ['app/Models/User.php']], $this->subject->output($tree));
628+
}
629+
598630
public static function modelTreeDataProvider(): array
599631
{
600632
return [
@@ -610,6 +642,7 @@ public static function modelTreeDataProvider(): array
610642
['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/Models/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'],
611643
['drafts/model-with-meta.yaml', 'app/Models/Post.php', 'models/model-with-meta.php'],
612644
['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'],
645+
['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'],
613646
];
614647
}
615648

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
User:
3+
id: uuid primary
4+
name: string
5+
base_pay: decimal:10,2
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Concerns\HasUuids;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class User extends Model
10+
{
11+
use HasFactory, HasUuids;
12+
13+
/**
14+
* The attributes that are mass assignable.
15+
*
16+
* @var array
17+
*/
18+
protected $fillable = [
19+
'name',
20+
'base_pay',
21+
];
22+
23+
/**
24+
* The attributes that should be cast to native types.
25+
*
26+
* @var array
27+
*/
28+
protected $casts = [
29+
'base_pay' => 'decimal:2',
30+
];
31+
}

0 commit comments

Comments
 (0)