Seeding
Seeding is simply a way to quickly seed, or put data into your tables.
Creating Seeds
You can create a seed file and seed class which can be used for keeping seed information and running it later.
To create a seed run the command:
This will create some boiler plate for your seeds that look like this:
From here you can start building your seed.
Building Your Seed
A simple seed might be creating a specific user that you use during testing.
Running Seeds
You can easily run your seeds:
Database Seeder
Factories
Factories are simple and easy ways to generate mass amounts of data quickly. You can put all your factories into a single file.
Creating A Factory Method
Factory methods are simple methods that take a single Faker
instance.
For methods available on the faker
variable reference the Faker documentation.
Registering Factories
Once created you can register the method with the Factory
class:
Naming Factories
If you need to you can also name your factories so you can use different factories for different use cases:
Calling Factories
To use the factories you can import the Factory
class from where you built your factories. In our case it was the config/factories.py
file:
This will persist these users to the database. If you want to simply make the models or collection (and not persist them) then use the make
method:
Again this will NOT persist values to the database.
Calling Named Factories
By default, Masonite will use the factory you created without a name. If you named the factories you can call those specific factories easily:
After Creating
You can also specify a second factory method that will run after a model is created. This would look like:
Now when you create a user it will be passed to this after_creating
method:
Modifying Factory Values
If you want to modify any values you previously set in the factory you created, you can pass a dictionary into the create
or make
method:
This is a great way to make constant values when testing that you can later assert to.
Last updated