Skip to content

Commit

Permalink
More features.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Jan 22, 2024
1 parent 59632a0 commit a6ef841
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions docs/model-properties/seeders.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function run()
}
```

Each Model Seeder is supercharged from a custom seeder from Larawiz, to make your development easier. This way you can seed a model separately, skip seeders, and even stop the whole seeding.
Each Model Seeder is supercharged from a custom seeder from Larawiz with some handy methods to make your development easier. This way you can seed a model separately, skip seeders, and even stop the whole seeding.

The class itself is very self-explanatory, here is an example for a hypothetical `Podcast` model.

Expand Down Expand Up @@ -97,7 +97,7 @@ public function seedCustomPodcasts(PodcastFactory $factory)
}
```

Finally, if you return anything that is `iteratable`, like an `array`, `Generator` or Collection, it will be used to _insert_ that data directly into the Seeder model table in one query.
Finally, if you return anything that is `iteratable`, like an `array`, `Generator` or Collection, it will be used to _insert_ that data directly into the model table in one query.

```php
use Faker\Generator as Faker;
Expand All @@ -107,8 +107,11 @@ protected string $model = Comment::class;

public function seedCommentsDirectlyToDatabase(Faker $faker)
{
foreach (Podcast::inRandomOrder()->take(500)->lazy() as $podcast) {
yield ['comment' => $faker->text(), 'podcast_id' => $podcast_id];
foreach (Podcast::inRandomOrder()->take(2000)->lazy() as $podcast) {
yield [
'comment' => $faker->text(),
'podcast_id' => $podcast_id
];
}
}
```
Expand All @@ -123,7 +126,7 @@ If you don't want to run transactions at all, set the `$transactions` property t
/**
* If the seeder should use transactions.
*
* @var bool|null
* @var bool
*/
protected bool $transactions = false;
```
Expand Down Expand Up @@ -153,6 +156,11 @@ class PodcastSeeder extends Seeder
Podcast::query()->truncate();
}

public function seed(PodcastFactory $factory)
{
// ...
}

public function after()
{
// Check if the podcasts were created as we expect, bail if not
Expand Down Expand Up @@ -200,63 +208,62 @@ public function before()
}
}
```
::: warning Skip or die
When methods [run within a transactions](#transactions), skipping will _roll back_ all changes made since it uses a `RuntimeException` to exit the function.

> Skips everything
>
> When methods [run within a transactions](#transactions), skipping will _roll back_ all changes made. Instead, divide your logic on more seeders, and put the skipping logic before any database manipulation.
If you need to skip after a certain point, just `return` or divide your logic on more skip-able seeders.
:::

## Calling other seeders with arguments

As you may (correctly) suspect, using `call()` with parameters works differently since the Larawiz Seeder overrides the `run()` call.

Instead, when you pass parameters, these are passed down to all `seed()` methods.
When you pass an array parameters, these are passed down to all `seed()` methods.

```php{11,17}
```php{11,16}
use App\Models\User;
use Database\Factories\UserFactory;
use Database\Factories\KarmaFactory;
class UserSeeder extends Seeder
class DatabaseSeeder extends Seeder
{
public function seedUser(UserFactory $factory)
public function run()
{
$user = $factory->create()
$user = User::factory()->create();
$this->call(KarmaSeeder::class, ['user' => $user])
$this->call(KarmaSeeder::class, ['user' => $user]);
}
}
class KarmaSeeder extends Seeder
{
public function seedKarma(KarmaFactory $factory, $user)
{
$factory->for($user)->times(2)->create(['amount' => $user->karma / 2])
$factory->for($user)->times(2)->create(['amount' => $user->karma / 2]);
}
}
```

If you need to pass parameters to specific seeding methods, just issue them with the method name as key and the parameters as an array.

```php{11-13,19}
```php{10-12,18}
use App\Models\User;
use Database\Factories\UserFactory;
use Database\Factories\KarmaFactory;
class UserSeeder extends Seeder
class DatabaseSeeder extends Seeder
{
public function seedUser(UserFactory $factory)
public function run()
{
$user = $factory->create()
$user = User::factory()->create();
$this->call(KarmaSeeder::class, [
'seedKarma' => ['user' => $user]
])
]);
}
}
class KarmaSeeder extends Seeder
{
public function seedKarma(KarmaFactory $factory, User $user)
public function seedKarma(KarmaFactory $factory, $user)
{
$factory->for($user)->times(2)->create(['amount' => $user->karma / 2])
}
Expand All @@ -275,4 +282,4 @@ models:
seeder: false
```

I mean, really? I just dumped 150 lines of code to make your development easier, and you dare to disable the seeder? Not cool man, not cool.
I mean, really? I just dumped almost 200 lines of code to make your development easier, and you dare to disable the seeder? Not cool man, not cool.

0 comments on commit a6ef841

Please sign in to comment.