Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
So why do we need to use Factories and Seeders? Imagine you wanted to add some test data for our API, but you don’t want to manually define all of the individual attributes for each model, which can be a tedious development task. Instead, we’re going to use model factories to generate large amounts of database records for each model instance using the powerful Faker library. Let’s get started!
[SOUND] Welcome back.
0:00
We're making significant progress.
0:02
And now that we've created our models and
migrations,
0:04
we're ready to dive into factories and
seeders.
0:08
So why do we need to use factories and
seeders?
0:11
Imagine you want to add some test data for
our API but
0:15
you don't want to manually define
all of the individual attributes for
0:19
each model,
which can be a tedious development task.
0:24
Instead, we're going to use model
factories to generate large amounts of
0:28
database records for each model instance
using the powerful Faker library.
0:33
To learn more about factories and seeders,
check out the teachers notes below.
0:39
Let's get started.
0:44
The factories, migrations and the seeds
folders are inside of the app/database
0:47
directory, where you'll also find
the DatabaseSeeder.php file.
0:53
Which we'll populate with to seeders,
AuthorsTableSeeder and
0:58
BooksTableSeeder, using two
artisan commands like this.
1:02
To test our connection,
use the php artisan migrate:fresh command.
1:14
As you can see, we now have two
seeders in our seeds directory.
1:22
AuthorsTableSeeder and BooksTableSeeder.
1:27
We're making great progress, and
1:31
we're ready to test our seeders by
using the db:seed command, like this.
1:33
If everything went well,
you should see a message that reads,
1:45
database seeding completed successfully.
1:49
Also note that they're both empty at
the moment, but that's because we
1:52
have not specified values for each column,
which we will do later in the course.
1:56
Finally, let's add both seeders to
the DatabaseSeeder.php file inside
2:02
of the run function, like this.
2:06
Currently, our seeders are empty
because we haven't defined them yet.
2:15
So let's open the UserFactory.php file,
which is a default Laravel factory.
2:19
And let's take a look at the defined
user factory data inside.
2:26
As you can see,
the test data has defined the name, email,
2:30
email_verified_at, password and
remember_token attributes,
2:35
which is similar to how we're going to
define the author and book factories.
2:42
Let's go ahead and
2:48
delete the userfactory.php file since we
won't be using that file in this project.
2:50
Now that we've created the author and
book seeders and
2:56
added them to the DatabaseSeeder.php file,
3:00
we're ready to specify some test data for
the AuthorTableSeeder and
3:03
BooksTableSeeder files inside
of the public run function.
3:08
Inside of the public run function,
we'll specify values for each column.
3:13
However, instead of manually
specifying the test data for
3:18
each column, we can use model factories
and a robust library named Faker.
3:23
Next, let's open the
AuthorsTableSeeder.php file to take a look
3:29
at the public run function inside,
which, again, is currently empty.
3:34
Inside of the run function,
let's add our defined seed data for
3:40
each column in our database tables.
3:45
First, create two factories,
the author factory and
3:47
book factory,
using these two artisan commands.
3:52
Ctrl+K will clear the terminal,
3:56
php artisan make:factory AuthorFactory.
4:01
And,
4:13
Next, open the AuthorFactory.php file and
4:32
change Model::class to Author::class, and
4:36
modify the import at the top from
use App\Model to use App\Author.
4:40
Next, let's open the authors_table
migration, so we can define the seed
4:49
data while looking at the database
tables side by side, like this.
4:54
Before we move on to defining the book
factory, we need to connect the author
5:27
factory to the AuthorsTableSeeder file to
create three author instances, like this.
5:32
Next, let's test our AuthorsTableSeeder
using the php artisan db:seed command.
5:44
If everything went well,
you'd see a message that says,
6:09
database seeding completed successfully.
6:13
And more importantly,
6:16
you'd see that the AuthorsTableSeeder
took a little bit longer to complete,
6:17
in my case, 0.5 seconds, but this is
because we generated some test data.
6:22
In comparison, the BooksTableSeeder
completed in 0 seconds because
6:27
there was no test data to generate.
6:32
Way to go.
6:34
Next, let's apply the same treatment to
6:36
the BookTableSeeder by opening
the BookFactory.php file,
6:40
then change Model::Class to Book::class,
and
6:46
modify the import at the top from
use App\Model to use App\Book.
6:51
Next, let's open the Books table so
6:57
we can define the seed data while looking
at the database tables side by side.
7:00
Finally, we need to connect the book
factory to the BooksTableSeeder.php file,
7:17
as we did with the AuthorsTableSeeder.php
file in the public run function,
7:23
like this.
7:28
To test our work,
7:31
let's use the php artisan migrate fresh
command to wipe out the previous data.
7:32
Followed by the php artisan db:seed
command to reseed our test data.
7:46
If all went well, you should see
messages that look like this.
7:53
Way to go
7:58
You need to sign up for Treehouse in order to download course files.
Sign up