Skip to content

Commit dc4a5f0

Browse files
committed
v3
1 parent 3837b84 commit dc4a5f0

File tree

6 files changed

+28
-79
lines changed

6 files changed

+28
-79
lines changed

readme.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ This package works fine alongside traditional Laravel migration files, for the c
1313
- [Running Migrations](#running-migrations)
1414
- [Migration Order](#migration-order)
1515
- [Publishing Stubs](#publishing-stubs)
16-
- [Traits](#traits)
17-
- [HasHashes](#hashashes)
1816

1917
## Installation
2018

@@ -110,23 +108,3 @@ Update the `stub_path` in `config/laravel-automatic-migrations.php`:
110108
```
111109

112110
Now edit the stub files inside `resources/stubs/vendor/laravel-automatic-migrations`. Commands will now use these stub files to make models and factories.
113-
114-
## Traits
115-
116-
### HasHashes
117-
118-
This trait will automatically hash attributes specified via a `$hashes` property in your model. It will only do so if the values are not already hashed, so it does not slow down seeders.
119-
120-
```php
121-
namespace App\Models;
122-
123-
use Bastinald\LaravelAutomaticMigrations\Traits\HasHashes;
124-
use Illuminate\Foundation\Auth\User as Authenticatable;
125-
126-
class User extends Authenticatable
127-
{
128-
use HasHashes;
129-
130-
protected $hashes = ['password'];
131-
}
132-
```

resources/stubs/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function migration(Blueprint $table)
2424
public function definition(Generator $faker)
2525
{
2626
return [
27-
'name' => $faker->name,
28-
'created_at' => $faker->dateTimeThisMonth,
27+
'name' => $faker->name(),
28+
'created_at' => $faker->dateTimeThisMonth(),
2929
];
3030
}
3131
}

resources/stubs/UserModel.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace DummyModelNamespace;
44

5-
use Bastinald\LaravelAutomaticMigrations\Traits\HasHashes;
65
use Faker\Generator;
76
use Illuminate\Database\Eloquent\Factories\HasFactory;
87
use Illuminate\Database\Schema\Blueprint;
@@ -12,10 +11,9 @@
1211

1312
class DummyModelClass extends Authenticatable
1413
{
15-
use HasFactory, HasHashes, Notifiable;
14+
use HasFactory, Notifiable;
1615

1716
protected $guarded = [];
18-
protected $hashes = ['password'];
1917
protected $hidden = ['password', 'remember_token'];
2018
protected $casts = ['email_verified_at' => 'datetime'];
2119

@@ -24,23 +22,22 @@ public function migration(Blueprint $table)
2422
$table->id();
2523
$table->string('name');
2624
$table->string('email')->unique();
25+
$table->timestamp('email_verified_at')->nullable();
2726
$table->string('password');
2827
$table->rememberToken();
29-
$table->string('timezone')->nullable();
30-
$table->timestamp('email_verified_at')->nullable();
3128
$table->timestamp('created_at')->nullable();
3229
$table->timestamp('updated_at')->nullable();
3330
}
3431

3532
public function definition(Generator $faker)
3633
{
3734
return [
38-
'name' => $faker->firstName,
39-
'email' => $faker->unique()->safeEmail,
35+
'name' => $faker->name(),
36+
'email' => $faker->unique()->safeEmail(),
37+
'email_verified_at' => now(),
4038
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
4139
'remember_token' => Str::random(10),
42-
'timezone' => $faker->timezone,
43-
'created_at' => $faker->dateTimeThisMonth,
40+
'created_at' => $faker->dateTimeThisMonth(),
4441
];
4542
}
4643
}

src/Commands/MakeAModelCommand.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function handle()
1919
$this->filesystem = new Filesystem;
2020

2121
$this->modelParser = new ComponentParser(
22-
is_dir(app_path('Models')) ? 'App\\Models' : 'App',
22+
'App\\Models',
2323
config('livewire.view_path'),
2424
$this->argument('class')
2525
);
@@ -37,37 +37,36 @@ public function handle()
3737
return;
3838
}
3939

40-
$this->deleteUserMigrations();
40+
$this->deleteUserMigration();
4141
$this->makeStubs();
4242

4343
$this->line('<info>Model created:</info> ' . $this->modelParser->relativeClassPath());
4444
$this->line('<info>Factory created:</info> ' . $this->factoryPath('relativeClassPath'));
4545
}
4646

47-
private function deleteUserMigrations()
47+
private function deleteUserMigration()
4848
{
49-
if ($this->modelParser->className() == 'User') {
50-
$names = ['create_users_table', 'add_timezone_column_to_users_table'];
49+
if ($this->modelParser->className() != 'User') {
50+
return;
51+
}
5152

52-
foreach ($this->filesystem->allFiles(database_path('migrations')) as $file) {
53-
if (Str::contains($file, $names)) {
54-
$path = 'database/migrations/' . $file->getRelativePathname();
53+
$path = 'database/migrations/2014_10_12_000000_create_users_table.php';
54+
$file = base_path($path);
5555

56-
$this->filesystem->delete($file);
56+
if ($this->filesystem->exists($file)) {
57+
$this->filesystem->delete($file);
5758

58-
$this->line('<info>Migration deleted:</info> ' . $path);
59-
}
60-
}
59+
$this->line('<info>Migration deleted:</info> ' . $path);
6160
}
6261
}
6362

6463
private function makeStubs()
6564
{
65+
$prefix = $this->modelParser->className() == 'User' ? 'User' : null;
66+
6667
$stubs = [
67-
$this->modelParser->classPath() =>
68-
$this->modelParser->className() == 'User' ? 'UserModel.php' : 'Model.php',
69-
$this->factoryPath('classPath') =>
70-
$this->modelParser->className() == 'User' ? 'UserFactory.php' : 'Factory.php',
68+
$this->modelParser->classPath() => $prefix . 'Model.php',
69+
$this->factoryPath('classPath') => $prefix . 'Factory.php',
7170
];
7271

7372
$replaces = [

src/Commands/MigrateAutoCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public function handle()
2222
return;
2323
}
2424

25-
$this->runTraditionalMigrations();
26-
$this->runAutomaticMigrations();
25+
$this->handleTraditionalMigrations();
26+
$this->handleAutomaticMigrations();
2727
$this->seed();
2828

2929
$this->info('Automatic migration completed successfully.');
3030
}
3131

32-
private function runTraditionalMigrations()
32+
private function handleTraditionalMigrations()
3333
{
3434
$command = 'migrate';
3535

@@ -44,9 +44,9 @@ private function runTraditionalMigrations()
4444
Artisan::call($command, [], $this->getOutput());
4545
}
4646

47-
private function runAutomaticMigrations()
47+
private function handleAutomaticMigrations()
4848
{
49-
$path = is_dir(app_path('Models')) ? app_path('Models') : app_path();
49+
$path = app_path('Models');
5050
$namespace = app()->getNamespace();
5151
$models = collect();
5252

src/Traits/HasHashes.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)