Remember, no Artisan
Forget creating each file from scratch.
diff --git a/404.html b/404.html index f0396dd..90545e5 100644 --- a/404.html +++ b/404.html @@ -13,7 +13,7 @@
404
But if you don't change your direction, and if you keep looking, you may end up where you are heading.
Seeders are conveniently created automatically for the model if their Factory is enabled, otherwise they won't be created.
Larawiz will register the seeders in your database/seeds/DatabaseSeeder.php
in the order these appear in your blueprint, so you won't have to register them manually.
// database/factories/DatabaseSeeder.php
+import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{"title":"Seeders","description":"","frontmatter":{},"headers":[{"level":2,"title":"Returning data","slug":"returning-data"},{"level":2,"title":"Transactions","slug":"transactions"},{"level":2,"title":"Before & After","slug":"before-after"},{"level":2,"title":"Skipping a seeder","slug":"skipping-a-seeder"},{"level":2,"title":"No Model Seeder","slug":"no-model-seeder"}],"relativePath":"model-properties/seeders.md"}'),p={name:"model-properties/seeders.md"},e=l(`Seeders
Model Seeders are conveniently created when the model Factory is enabled. If the factory has been disabled, they won't be included.
For starters, Larawiz registers the seeders in your database/seeds/DatabaseSeeder.php
file, in the order these appear in your blueprint, so you don't have to.
php// database/factories/DatabaseSeeder.php
public function run()
{
@@ -6,7 +6,7 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
$this->call(PodcastSeeder::class);
$this->call(SubscriptionSeeder::class);
}
-
1
2
3
4
5
6
7
8
Larawiz ships with a supercharged seeder made to make your development easier, and each seeder extends that.
Seeding works better than the Laravel version by calling all public methods that start with seed
, and in the order these appear in the class.
The class itself is very self-explanatory, here is an example for a hypothetical Podcast
model.
yamlmodels:
+
1
2
3
4
5
6
7
8
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.
The class itself is very self-explanatory, here is an example for a hypothetical Podcast
model.
yamlmodels:
Podcasts:
# ...
1
2
3
php<?php
@@ -17,11 +17,6 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
use App\\Models\\Podcast;
use Database\\Factories\\PodcastFactory;
-/**
- * This seeder will run all methods that start with 'seed'.
- *
- * @extends \\Database\\Seeders\\BaseSeeder<\\App\\Models\\Podcast>
- */
class PodcastSeeder extends BaseSeeder
{
/**
@@ -39,7 +34,7 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
$factory->count($this->pages(2.5))->create();
}
}
-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Each seed
method is resolved by the application container, so you can get use Dependency Injection to get anything you need. For example, we can later create a seeder for deleted podcasts.
phpuse Faker\\Generator as Faker;
+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Your Model Seeder executes all method that start with seed
, and each method is resolved by the application container to take advantage of Dependency Injection. For example, you may add another seeding procedure for trashed models and call the Faker
generator for some random text.
phpuse Faker\\Generator as Faker;
use Database\\Factories\\PodcastFactory;
/**
@@ -48,26 +43,26 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
public function seedTrashedPodcasts(PodcastFactory $factory, Faker $faker): void
{
$factory->trashed()->create([
- 'excerpt' => $faker->text()
+ 'trashed_reason' => $faker->text()
]);
}
-
1
2
3
4
5
6
7
8
9
10
11
12
Return
If you return a factory from the seed
method, the Seeder will automatically create the records for you.
phpuse Database\\Factories\\PodcastFactory;
+
1
2
3
4
5
6
7
8
9
10
11
12
Returning data
If you return a Model Factory from the seeding method, the Seeder will automatically create the records for you.
phpuse Database\\Factories\\PodcastFactory;
public function seedCustomPodcasts(PodcastFactory $factory)
{
return $factory->custom()->state(['coolness' => 'high'])->count(20);
}
-
1
2
3
4
5
6
If you return an Eloquent Collection, it will persist all models on it:
phpuse Database\\Factories\\PodcastFactory;
+
1
2
3
4
5
6
On the other hand, if you return an Eloquent Collection, it will persist all models and relations on it.
phpuse Database\\Factories\\PodcastFactory;
public function seedCustomPodcasts(PodcastFactory $factory)
{
$podcasts = $factory->custom()->makeMany();
- // Something with the podcasts...
+ // Do something with the podcasts...
return $podcasts;
}
-
1
2
3
4
5
6
7
8
9
10
Finally, if you return anything that is iteratable
, from an array
to a Generator
, it will be used to insert that data directly into the Seeder model table in one query.
phpuse Faker\\Generator as Faker;
+
1
2
3
4
5
6
7
8
9
10
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.
phpuse Faker\\Generator as Faker;
use App\\Models\\Podcast;
protected string $model = Comment::class;
@@ -78,13 +73,13 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
yield ['comment' => $faker->text(), 'podcast_id' => $podcast_id];
}
}
-
1
2
3
4
5
6
7
8
9
10
11
Transactions
All seed
methods are called within a Database Transaction. This to avoids the development hell of orphaned or incomplete records when a seeder throws an error. It also speeds up seeding under SQLite.
If you don't want to run transactions at all, set the $transactions
property to false.
php/**
+
1
2
3
4
5
6
7
8
9
10
11
Transactions
All seeding methods are called within a Database Transaction to avoid the development hell of orphaned or incomplete records when a seeder throws an error in the middle of the operation. It also speeds up seeding under SQLite when using a file-based database.
If you don't want to run transactions at all, set the $transactions
property to false.
php/**
* If the seeder should use transactions.
*
* @var bool|null
*/
protected bool $transactions = false;
-
1
2
3
4
5
6
Before & After
You may call a method before and after the seeding by just creating them as before()
and after()
, respectively. As with the seed methods, these are resolved by the container, so you can use Dependency Injection.
The before()
method is a great place to make checks or prepare the database beforehand, while the after()
is a good place to remove artifacts or check everything when right.
phpuse App\\Models\\Podcast;
+
1
2
3
4
5
6
Before & After
You may execute some logic before and after the calling all the seeding methods by just creating the before()
and after()
methods, respectively. As with the seed methods, these are resolved by the container, so you can use Dependency Injection.
The before()
method is a great place to skip the seeder class or prepare the database beforehand. The after()
is good to remove unwanted artifacts or just stop if something is not right.
phpuse App\\Models\\Podcast;
public function before(SomethingCool $cool)
{
@@ -107,7 +102,7 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
// Remove all podcasts that have a banned author
Podcast::whereHas('author', fn($query) => $query->whereNotNull('banned_at'))->delete();
}
-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Skipping a seeder
Sometimes you may have a seeder you want to skip for different reasons, like when some records already exist, or you already run it after a prior seeding run failed.
To stop a seed method and continue to the next, invoke the use skip()
method or the convenience methods skipIf()
and skipUnless()
. If you use the Eloquent Query Builder instance, it will automatically check if a record exists as condition.
phpuse App\\Models\\Podcast;
+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Skipping a seeder
Sometimes you may have a seeder you want to skip for different reasons, like when some records already exist, or you already run it after a prior seeding run failed.
To stop a seed method and continue to the next, invoke the use skip()
method or the convenience methods skipIf()
and skipUnless()
. If you use an Eloquent Query Builder instance, it will check if there is any existing record for that query.
phpuse App\\Models\\Podcast;
public function seedTrashedPodcasts(PodcastFactory $factory)
{
@@ -119,7 +114,7 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
// Or simplified...
$this->skipIf(Podcast::whereNotNull('deleted_at'));
}
-
1
2
3
4
5
6
7
8
9
10
11
12
To skip the whole seeder, invoke the skip
on the before()
method.
phpuse Illuminate\\Support\\Facades\\DB;
+
1
2
3
4
5
6
7
8
9
10
11
12
To skip the whole seeder, invoke the skip()
on the before()
method, so
phpuse Illuminate\\Support\\Facades\\DB;
public function before()
{
@@ -128,9 +123,9 @@ import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{
$this->skip();
}
}
-
1
2
3
4
5
6
7
8
9
No Model Seeder
To disable the seeder for a given model, set the seeder
as false
.
yamlmodels:
+
1
2
3
4
5
6
7
8
9
Skips everything
When methods run within a transactions, skipping will roll back all changes made. Instead, divide your logic on more seeders, and put the skipping logic before any database manipulation.
No Model Seeder
To disable the seeder for a given model before checking your mental health, set the seeder
as false
.
yamlmodels:
Podcast:
# ...
seeder: false
-
1
2
3
4
5
`,35),o=[e];function r(c,t,F,D,y,i){return a(),n("div",null,o)}const d=s(p,[["render",r]]);export{A as __pageData,d as default};
+
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.
`,36),o=[e];function r(c,t,F,y,D,i){return a(),n("div",null,o)}const d=s(p,[["render",r]]);export{A as __pageData,d as default}; diff --git a/assets/model-properties_seeders.md.4f48f7e7.lean.js b/assets/model-properties_seeders.md.4f48f7e7.lean.js new file mode 100644 index 0000000..9cbbfdb --- /dev/null +++ b/assets/model-properties_seeders.md.4f48f7e7.lean.js @@ -0,0 +1 @@ +import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{"title":"Seeders","description":"","frontmatter":{},"headers":[{"level":2,"title":"Returning data","slug":"returning-data"},{"level":2,"title":"Transactions","slug":"transactions"},{"level":2,"title":"Before & After","slug":"before-after"},{"level":2,"title":"Skipping a seeder","slug":"skipping-a-seeder"},{"level":2,"title":"No Model Seeder","slug":"no-model-seeder"}],"relativePath":"model-properties/seeders.md"}'),p={name:"model-properties/seeders.md"},e=l("",36),o=[e];function r(c,t,F,y,D,i){return a(),n("div",null,o)}const d=s(p,[["render",r]]);export{A as __pageData,d as default}; diff --git a/assets/model-properties_seeders.md.8a17caae.lean.js b/assets/model-properties_seeders.md.8a17caae.lean.js deleted file mode 100644 index 1ab4b12..0000000 --- a/assets/model-properties_seeders.md.8a17caae.lean.js +++ /dev/null @@ -1 +0,0 @@ -import{_ as s,c as n,o as a,a as l}from"./app.70f38bd9.js";const A=JSON.parse('{"title":"Seeders","description":"","frontmatter":{},"headers":[{"level":2,"title":"Return","slug":"return"},{"level":2,"title":"Transactions","slug":"transactions"},{"level":2,"title":"Before & After","slug":"before-after"},{"level":2,"title":"Skipping a seeder","slug":"skipping-a-seeder"},{"level":2,"title":"No Model Seeder","slug":"no-model-seeder"}],"relativePath":"model-properties/seeders.md"}'),p={name:"model-properties/seeders.md"},e=l("",35),o=[e];function r(c,t,F,D,y,i){return a(),n("div",null,o)}const d=s(p,[["render",r]]);export{A as __pageData,d as default}; diff --git a/example.html b/example.html index 3d193ac..8c46de5 100644 --- a/example.html +++ b/example.html @@ -284,7 +284,7 @@ // ... } }All of that and much more!
Scaffold a Laravel app in 1 minute
Save hours syncing changes between models, migrations, and what else.
Forget creating each file from scratch.
Simple YAML creates all the proyect files in sync.
Larawiz does most of the usual model chores for you.
Set the relation, receive the methods, migrations and pivots.