Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Build a REST API with Laravel!
You have completed Build a REST API with Laravel!
Preview
Now that we’ve created a new Laravel application, added the database credentials to the .env file, and tested our database connection with php artisan migrate, we’re ready to start building our database migrations.
Migrations
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:00
Now that we've created a new Laravel
application, added the database
0:02
credentials to the ENV file, and
tested our database connection with
0:06
php artisan migrate, we're ready to
start building our database migrations.
0:11
So what are migrations exactly?
0:17
Migrations are like version control for
your database, allowing you and
0:21
your team to collaborate on the same
database schema without conflicts.
0:26
Migrations work in tandem with
a schema builder which defines you
0:31
application's database schema.
0:35
It's also important to mention
that the Laravel schema facade
0:39
provides a database-agnostic
support when working with tables,
0:43
which we'll import and
use later in the course.
0:47
Check out the Teacher's Notes
to learn more.
0:51
Are you ready?
0:54
Let's get started.
0:55
Now that we've successfully tested our
database connection, let's navigate to
0:57
the database/migrations directory and
delete the default migrations inside.
1:02
Next, let's use artisan to create our
models and migrations one at a time.
1:11
It's worth noting that we could use
the -mcr flag to create a model,
1:19
controller, and resource controller
all at once, with one command.
1:24
However, to demonstrate each step,
we'll build models and
1:30
migrations using just one flag,
-m, like this.
1:38
Finally, let's navigate to
the migrations directory and
1:52
verify that our new
migrations have been created.
1:55
Now that we've created our models and
migrations, let's take a look at our
2:01
models, then delete the user model,
which we won't be using in this course.
2:05
Next, modify the database schema
to accommodate the data for
2:11
our API, like this.
2:15
First, open the authors_table migration.
2:18
And let's keep the table
bigIncrements column,
2:22
which gives us an ID for each record.
2:26
Let's also keep timestamps so we know
when an add, update, or delete occurs.
2:30
Next, let's
2:37
add name,
2:46
title,
2:54
company, and
2:59
emai,l like
3:09
this.
3:18
Did you notice the down function
at the bottom of the page?
3:34
Let's take a look.
3:39
The differences between the up and
down functions are simple.
3:40
In Laravel, the up function will
run when you use the php artisan
3:45
migrate command and the down function
will run when you use the php
3:50
artisan migrate:rollback command.
3:55
It's also worth noting that
php artisan migrate:refresh
3:59
allows you to roll back and rerun all
of your migrations with one command.
4:05
If you want to drop all tables instead
of rolling them back before you rerun
4:10
migrations, use the php artisan
migrate:fresh command instead.
4:14
To learn more about these useful migration
commands, check the Teacher's Notes for
4:22
more information.
4:26
Nice job so far.
4:28
Let's repeat these same steps by opening
books_table migration, like this.
4:30
We'll also use timestamps,
like we did with the author model, so
4:37
we know when an add,
update, or delete occurs.
4:41
Next, let's add title author_id
in abstract, like this.
4:46
Note that we are using big integer
instead of using big increments,
4:53
this will allow us to have an ID and
an author ID.
4:58
Otherwise we would get an error
about duplicate primary keys.
5:02
Finally, longText is the data type we will
use for preview or excerpt for our books.
5:08
To test our migrations,
run the php artisan
5:15
migrate:fresh command to drop all
tables and rerun our migrations.
5:19
If everything went well, you would
see a successful migration message.
5:26
If not, try to debug the code yourself,
as typos are a common reason for
5:31
an error at this stage, or
5:36
feel free to reach out to other students
in the community if you still need help.
5:39
Way to go.
5:43
We learned about artisan flags and
how they can speed up development
5:45
when creating models, controllers,
and resource controllers.
5:49
We updated the database schema to
display the correct information for
5:55
authors and books.
5:59
Remember, if you have any
questions about artisan flags or
6:01
other parts of this course, feel free
to reach out to the Treehouse staff or
6:05
other students in the community
at teamtreehouse.com/community.
6:11
In the next section, we're going to
connect our database tables to our
6:16
models so we can define the author and
book relationships.
6:21
See you there.
6:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up