This course will be retired on January 24, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Database Setup
-
0:00
[SOUND] In order to keep persistent data for
-
0:03
our application, we're gonna need to integrate a database.
-
0:09
Laravel takes this normally verbose process and simplifies
-
0:13
it, without taking away any of the power.
-
0:16
To start with, we're gonna get our local database
-
0:18
setup, and then integrate it within our Laravel application.
-
0:23
Now we have our routes, we have our controller, we have actions.
-
0:26
We have everything we need to get going on
-
0:29
creating a new form and storing that to a database.
-
0:34
But in order to store it to the database, we're gonna actually need the database.
-
0:38
So let's go ahead and get our database setup now.
-
0:41
We're gonna start by connecting and creating a database manually.
-
0:45
I'm gonna switch over to a tool.
-
0:46
I'm going to use SQLPro, and then we're
-
0:48
going to move to connecting the database to Laravel.
-
0:52
So, let's get started by connecting manually here in our terminal by going to
-
0:57
our project, and then doing a shell connection to our vagrant box, so vagrant.
-
1:04
Ssh, okay,so here we're gonna connect using our MySQL command.
-
1:12
Okay, and that will get us to our MySQL console, but we're gonna
-
1:14
need to pass through a username and password in order to access this.
-
1:19
So it'll be a -u for the flag, and then
-
1:21
the user, how do we find our username and password.
-
1:24
Let's go back over to the Homestead Documentation.
-
1:28
So, under docs, under Homestead, here you'll see Daily Usage, go
-
1:33
to Daily Usage and then you'll see Connecting To Your Databases.
-
1:37
So it tells we've already got a Homestead database created.
-
1:40
We have Laravel local database configuration is
-
1:42
already set to use that database by default.
-
1:45
Were gonna change all of that, but what
-
1:47
we care about is connecting to the actual database.
-
1:50
So, we would need to connect using username and password.
-
1:52
Here it says is homestead and secret, lets try that now.
-
1:58
[SOUND] Flag dash p.
-
2:00
And then it says that the password is secret.
-
2:04
Okay so now we are into the MySQL monitor.
-
2:08
And the first command that we're gonna run,
-
2:09
just to see what's here, is show databases.
-
2:16
Okay, so here we have information schema,
-
2:18
MySQL and performance schema which are standard.
-
2:21
And then the actual homestead database.
-
2:23
Now, we're going to create a completely different database to
-
2:26
use, and that database is going to be called odot.
-
2:30
So, let's go ahead and create that database here.
-
2:33
Or you can switch over to your tool of choice.
-
2:36
So what I'll do is create it here manually and then
-
2:37
show you how to do it on the MySQL tool SQLPro.
-
2:42
[SOUND] Okay now we're going to do the same thing.
-
2:46
Show databases, [SOUND] and now we see we have odot.
-
2:51
Okay let's exit out of here now that we know what our user name password is.
-
2:54
We know we have MySQL installed.
-
2:56
We know we have our databases setup how we want.
-
3:00
Let's switch over to a more graphical user interface based tool, SQLPro.
-
3:06
To exit the MySQL monitor just type exit, and then it says bye,
-
3:10
and then you may also exit homestead to go back to your console.
-
3:16
Okay.
-
3:16
So, opening up SQLPro we're gonna need to create a new connection.
-
3:19
What we're gonna do is actually create a
-
3:20
favorite because we're gonna access this quite often.
-
3:22
We're gonna call it Laravel homestead.
-
3:27
All right.
-
3:28
Here we're going to try to do a connection to Laravel homestead.
-
3:33
We'll do it with a standard.
-
3:34
We'll go to 127.0.0.1.
-
3:37
And then our username is homestead.
-
3:42
And our password is secret.
-
3:46
Okay and we don't need to specify the database.
-
3:48
Now port 3306 if we test the connection we're gonna get unable to connect.
-
3:52
So what we need to do is look over at the
-
3:54
documentation and you'll se at the very bottom of homestead Daily Usage.
-
3:59
Here are our ports.
-
3:59
So MySQL will need to connect at this port.
-
4:02
33060.
-
4:05
Try that.
-
4:06
Test the connection, connection succeeded so, let's go ahead and save our changes.
-
4:11
And then we're gonna connect.
-
4:14
Okay, so inside of SQLPro, we're gonna get this screen a little bit bigger.
-
4:19
Look at our databases and here you'll see we have homestead and odot.
-
4:22
So if we wanted to create our database here, cuz we've already created it before.
-
4:28
We're going to delete this database.
-
4:32
Okay?
-
4:32
Delete.
-
4:33
And then now you'll see we don't have that database.
-
4:35
So to create a new database, we'll do a,
-
4:38
go to the Database drop down and do Add Database.
-
4:41
And then we will use odot.
-
4:43
It can be the same as before, database and coding as default.
-
4:47
Hit Add, and now we have our homestead and odot.
-
4:52
We're only gonna be using odot.
-
4:54
So, here we have the structure, the content relations, so on so forth.
-
4:58
We have a lot of information we can add and we're gonna
-
5:00
go into SQLPro and add data once we've created our database manually.
-
5:05
Now that we've created our database manually, we've gotta here
-
5:08
our final step here is to connect this database to Laravel.
-
5:13
So we're gonna need to go back to our code,
-
5:16
and connect up our config database to this particular MySQL database.
You need to sign up for Treehouse in order to download course files.
Sign up