diff --git a/404.html b/404.html index f0396dd..90545e5 100644 --- a/404.html +++ b/404.html @@ -13,7 +13,7 @@
Skip to content

404

PAGE NOT FOUND

But if you don't change your direction, and if you keep looking, you may end up where you are heading.
- + diff --git a/assets/model-properties_seeders.md.8a17caae.js b/assets/model-properties_seeders.md.4f48f7e7.js similarity index 80% rename from assets/model-properties_seeders.md.8a17caae.js rename to assets/model-properties_seeders.md.4f48f7e7.js index 81401de..ca751df 100644 --- a/assets/model-properties_seeders.md.8a17caae.js +++ b/assets/model-properties_seeders.md.4f48f7e7.js @@ -1,4 +1,4 @@ -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(`

Seeders

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.

php
// 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.

yaml
models:
+
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.

yaml
models:
   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.

php
use 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.

php
use 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.

php
use 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.

php
use 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:

php
use 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.

php
use 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.

php
use 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.

php
use 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.

php
use 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.

php
use 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.

php
use 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.

php
use 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.

php
use 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

php
use 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.

yaml
models:
+
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.

yaml
models:
   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}; +
1
2
3
4
5

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 @@ // ... } }
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

All of that and much more!

- + diff --git a/hashmap.json b/hashmap.json index a587285..d74a09c 100644 --- a/hashmap.json +++ b/hashmap.json @@ -1 +1 @@ -{"example.md":"f8c7fc26","index.md":"a4cd4a3e","migrations.md":"313b97d2","model-columns_index.md":"b3e37c88","model-columns_indexes.md":"59b7216a","model-columns_primary-key.md":"eb40f6d8","model-columns_soft-deletes.md":"f85f1393","model-columns_timestamps.md":"743afc10","model-properties_appends.md":"da9ab46e","model-properties_casts.md":"ef255822","model-properties_factories.md":"36046bc0","model-properties_fillable.md":"6ca427ae","model-properties_hidden.md":"753a815c","model-properties_observers.md":"c8809ae2","model-properties_scopes.md":"1821b0f9","model-properties_seeders.md":"8a17caae","model-properties_table.md":"c6b52d9d","model-properties_traits.md":"d0084b56","model-properties_users.md":"8bbf19fe","model-relations_has-one-or-many-through.md":"2150fcde","model-relations_has-one-or-many.md":"d3d2f10f","model-relations_index.md":"f3597bb3","model-relations_many-to-many-and-polymorphic.md":"0b977274","model-relations_pivot-models.md":"0a41d37f","model-relations_polymorphic-has-one-or-many.md":"84e77b54","model.md":"a8f48a1e","scaffold.md":"aba75ae3"} +{"example.md":"f8c7fc26","index.md":"a4cd4a3e","migrations.md":"313b97d2","model-columns_index.md":"b3e37c88","model-columns_indexes.md":"59b7216a","model-columns_primary-key.md":"eb40f6d8","model-columns_soft-deletes.md":"f85f1393","model-columns_timestamps.md":"743afc10","model-properties_appends.md":"da9ab46e","model-properties_casts.md":"ef255822","model-properties_factories.md":"36046bc0","model-properties_fillable.md":"6ca427ae","model-properties_hidden.md":"753a815c","model-properties_observers.md":"c8809ae2","model-properties_scopes.md":"1821b0f9","model-properties_seeders.md":"4f48f7e7","model-properties_table.md":"c6b52d9d","model-properties_traits.md":"d0084b56","model-properties_users.md":"8bbf19fe","model-relations_has-one-or-many-through.md":"2150fcde","model-relations_has-one-or-many.md":"d3d2f10f","model-relations_index.md":"f3597bb3","model-relations_many-to-many-and-polymorphic.md":"0b977274","model-relations_pivot-models.md":"0a41d37f","model-relations_polymorphic-has-one-or-many.md":"84e77b54","model.md":"a8f48a1e","scaffold.md":"aba75ae3"} diff --git a/index.html b/index.html index 2e6c36e..18f5d40 100644 --- a/index.html +++ b/index.html @@ -14,7 +14,7 @@
Skip to content

Larawiz

Scaffold a Laravel app in 1 minute

Save hours syncing changes between models, migrations, and what else.

🙅‍♂️

Remember, no Artisan

Forget creating each file from scratch.

🤪

Brain-dead syntax

Simple YAML creates all the proyect files in sync.

🧙‍♂️

I'm a wizard, Harry

Larawiz does most of the usual model chores for you.

🤝

Easy relations

Set the relation, receive the methods, migrations and pivots.

- + diff --git a/migrations.html b/migrations.html index 8ff8a4f..90454dc 100644 --- a/migrations.html +++ b/migrations.html @@ -200,7 +200,7 @@ password_resets: false personal_access_tokens: false
1
2
3
4

They are as it is

The included migrations cannot be overridden with your own. You may do it after your application is scaffolded.

- + diff --git a/model-columns/index.html b/model-columns/index.html index 2333aa7..f09d955 100644 --- a/model-columns/index.html +++ b/model-columns/index.html @@ -46,7 +46,7 @@ $table->myCustomMethod(); });
1
2
3
4
5
6
7
8

Prefer the wavy thing

To issue a null value, use ~. Some YAML parsers run in circles when a value is empty.

- + diff --git a/model-columns/indexes.html b/model-columns/indexes.html index 099218c..42d04df 100644 --- a/model-columns/indexes.html +++ b/model-columns/indexes.html @@ -49,7 +49,7 @@ $table->unique(['slug', 'id'], 'slug_id_index'); });
1
2
3
4
5
6
7
8
9
10
11

The indexes will be created in the migrations file, and executed after the table is created.

Morphs are already indexed

Laravel migrations already create an index over the morph itself, so you don't need to declare an index over a morphTo relation.

- + diff --git a/model-columns/primary-key.html b/model-columns/primary-key.html index 5f9d8bf..8a9384b 100644 --- a/model-columns/primary-key.html +++ b/model-columns/primary-key.html @@ -146,7 +146,7 @@ $table->string('slug'); });
1
2
3
4

You may want to use Primary Keys anyway

A primary key identifies a single row in the database in a performant way. Laravel rely heavily on these.

Most of the time you will want one in your table, specially if you expect to make changes on a single record.

- + diff --git a/model-columns/soft-deletes.html b/model-columns/soft-deletes.html index 093d4ef..e31d942 100644 --- a/model-columns/soft-deletes.html +++ b/model-columns/soft-deletes.html @@ -64,7 +64,7 @@ } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- + diff --git a/model-columns/timestamps.html b/model-columns/timestamps.html index 832922e..1eb20c9 100644 --- a/model-columns/timestamps.html +++ b/model-columns/timestamps.html @@ -87,7 +87,7 @@ $table->timestamp('creation_date')->nullable(); });
1
2
3
4
5
6
7
- + diff --git a/model-properties/appends.html b/model-properties/appends.html index 7ecea86..f5887f0 100644 --- a/model-properties/appends.html +++ b/model-properties/appends.html @@ -56,7 +56,7 @@ } }
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
31
32

Little helps that count

When issuing timestamp|datetime or collection, Larawiz will automatically change the annotation to Carbon and Collection, respectively.

- + diff --git a/model-properties/casts.html b/model-properties/casts.html index 59ee8c4..35368ed 100644 --- a/model-properties/casts.html +++ b/model-properties/casts.html @@ -164,7 +164,7 @@ ]; }
1
2
3
4
5
6
7
8
9
10
11
- + diff --git a/model-properties/factories.html b/model-properties/factories.html index 6255310..8d3c700 100644 --- a/model-properties/factories.html +++ b/model-properties/factories.html @@ -105,7 +105,7 @@ }); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

States will be created empty with an exception to remember to setting the apropiate attributes.

- + diff --git a/model-properties/fillable.html b/model-properties/fillable.html index 653c54e..f2f8974 100644 --- a/model-properties/fillable.html +++ b/model-properties/fillable.html @@ -71,7 +71,7 @@ fillable: false
1
2
3
4
5
6
7
8
9
10
- + diff --git a/model-properties/hidden.html b/model-properties/hidden.html index 4c109e1..f1f5437 100644 --- a/model-properties/hidden.html +++ b/model-properties/hidden.html @@ -65,7 +65,7 @@ editor_notes: json hidden: false
1
2
3
4
5
6
7
8
- + diff --git a/model-properties/observers.html b/model-properties/observers.html index fcfdfc4..1a4a718 100644 --- a/model-properties/observers.html +++ b/model-properties/observers.html @@ -92,7 +92,7 @@ // ... }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

You can use external observers too

If you want to use an external observer, just append \ to the name, like \Vendor\Package\ExternalObserver. Larawiz won't create that observer, so you can add it through a third party package later.

- + diff --git a/model-properties/scopes.html b/model-properties/scopes.html index 1f12f8d..4a6bbb8 100644 --- a/model-properties/scopes.html +++ b/model-properties/scopes.html @@ -81,7 +81,7 @@ scopes: - Unpublished
1
2
3
4
5
6
7
8
9
10

Since you can do amazing things with Global Scopes, like applying them at runtime, Larawiz won't add it to your model, but you can apply the Global Scope manually.

- + diff --git a/model-properties/seeders.html b/model-properties/seeders.html index f4f6faf..b45bbe4 100644 --- a/model-properties/seeders.html +++ b/model-properties/seeders.html @@ -7,13 +7,13 @@ - + -
Skip to content
On this page

Seeders

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.

php
// database/factories/DatabaseSeeder.php
+    
Skip to content
On this page

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()
 {
@@ -21,7 +21,7 @@
     $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.

yaml
models:
+
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.

yaml
models:
   Podcasts:
   # ...
 
1
2
3
php
<?php
@@ -32,11 +32,6 @@
 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
 {
     /**
@@ -54,7 +49,7 @@
         $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.

php
use 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.

php
use Faker\Generator as Faker;
 use Database\Factories\PodcastFactory;
 
 /**
@@ -63,26 +58,26 @@
 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.

php
use 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.

php
use 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:

php
use 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.

php
use 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.

php
use 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.

php
use Faker\Generator as Faker;
 use App\Models\Podcast;
 
 protected string $model = Comment::class;
@@ -93,13 +88,13 @@
         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.

php
use 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.

php
use App\Models\Podcast;
 
 public function before(SomethingCool $cool)
 {
@@ -122,7 +117,7 @@
     // 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.

php
use 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.

php
use App\Models\Podcast;
 
 public function seedTrashedPodcasts(PodcastFactory $factory)
 {
@@ -134,7 +129,7 @@
     // 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.

php
use 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

php
use Illuminate\Support\Facades\DB;
 
 public function before()
 {
@@ -143,13 +138,13 @@
         $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.

yaml
models:
+
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.

yaml
models:
   Podcast:
     # ...
 
     seeder: false
-
1
2
3
4
5
- +
1
2
3
4
5

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.

+ diff --git a/model-properties/table.html b/model-properties/table.html index 926c603..106b5ea 100644 --- a/model-properties/table.html +++ b/model-properties/table.html @@ -70,7 +70,7 @@ // ... })
1
2
3
- + diff --git a/model-properties/traits.html b/model-properties/traits.html index f18ee03..f65fbae 100644 --- a/model-properties/traits.html +++ b/model-properties/traits.html @@ -95,7 +95,7 @@ // ... }
1
2
3
4
5
6
7
8
9
10
11
12
13
- + diff --git a/model-properties/users.html b/model-properties/users.html index 607226d..4cfd944 100644 --- a/model-properties/users.html +++ b/model-properties/users.html @@ -73,7 +73,7 @@ // ... }
1
2
3
4
- + diff --git a/model-relations/has-one-or-many-through.html b/model-relations/has-one-or-many-through.html index 5fdfd52..bf6e4f2 100644 --- a/model-relations/has-one-or-many-through.html +++ b/model-relations/has-one-or-many-through.html @@ -81,7 +81,7 @@ } }
1
2
3
4
5
6
7

Always on the safe side

Larawiz will make the columns and relations automatically into the models if you didn't declare it.

- + diff --git a/model-relations/has-one-or-many.html b/model-relations/has-one-or-many.html index e0bc344..605c673 100644 --- a/model-relations/has-one-or-many.html +++ b/model-relations/has-one-or-many.html @@ -131,7 +131,7 @@ } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- + diff --git a/model-relations/index.html b/model-relations/index.html index 3cc6336..7af0928 100644 --- a/model-relations/index.html +++ b/model-relations/index.html @@ -70,7 +70,7 @@ } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Primary Keys are required

Larawiz will break the wand if you make a relation to a Model without a primary key.

- + diff --git a/model-relations/many-to-many-and-polymorphic.html b/model-relations/many-to-many-and-polymorphic.html index 0dc703f..80c6432 100644 --- a/model-relations/many-to-many-and-polymorphic.html +++ b/model-relations/many-to-many-and-polymorphic.html @@ -163,7 +163,7 @@ $table->uuidMorphs('taggable'); });
1
2
3
4
5

Overriding automatic pivot tables

To override an automatic pivot table, you may create a migration for it. More details in the Migration section.

- + diff --git a/model-relations/pivot-models.html b/model-relations/pivot-models.html index 038717f..eead9f4 100644 --- a/model-relations/pivot-models.html +++ b/model-relations/pivot-models.html @@ -236,7 +236,7 @@ // ... }
1
2
3
4

No soft-deletes

Pivots models do not support soft-deletes.

Declaring softDeletes: ~ in the Pivot Model doesn't do anything, but there is a very low chance Larawiz will summon you into a Nickelback concert.

- + diff --git a/model-relations/polymorphic-has-one-or-many.html b/model-relations/polymorphic-has-one-or-many.html index 14706fd..004ddcf 100644 --- a/model-relations/polymorphic-has-one-or-many.html +++ b/model-relations/polymorphic-has-one-or-many.html @@ -170,7 +170,7 @@ $table->nullableMorphs('imageable'); });
1
2
3
4
- + diff --git a/model.html b/model.html index 7f8bbed..fcae3ec 100644 --- a/model.html +++ b/model.html @@ -103,7 +103,7 @@ - withoutFactory - available
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- + diff --git a/scaffold.html b/scaffold.html index 318cb96..f4e1ae8 100644 --- a/scaffold.html +++ b/scaffold.html @@ -20,7 +20,7 @@ cd my-blog php artisan serve
1
2
3

After that, your project will be created in the path of your Terminal with its name in kebab-case, so you can start development immediately.

The link is public

Larawiz generates your project using a hash. Anyone with the link to the hash will be able to download it.

- +