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
Now that we have our DB up, how do we connect it to our application?
-
0:00
Let's talk a little bit about configuration in environments.
-
0:04
One of the things inside of Laravel is it has the ability to do multiple
-
0:07
environments such as, in our case local or
-
0:10
production development testing, so on and so forth.
-
0:14
What we're doing now is currently working in the local environment.
-
0:18
There are two ways to test this.
-
0:19
One is that when we initially set up our homestead.
-
0:23
We had a default that said our environment was local.
-
0:27
We can actual switch over to our Vagrant box, so you have to Vagrant SSH.
-
0:30
And then go to your sites laravel-basics folder.
-
0:34
What you're going to do is type in
-
0:35
an artisan command, php artisan, and then, env.
-
0:40
And what that's going to do is
-
0:41
tell you what your current application environment is.
-
0:45
Why that mattered to us right now is as we're setting up our database
-
0:48
the local database config is usually different
-
0:51
than the say, the production database configuration.
-
0:53
So if you look in your config folder, you'll notice that there is a
-
0:58
subfolder called local, a subfolder called testing, and a subfolder called packages.
-
1:04
We're going to concern now with, local.
-
1:06
You'll see there is a local app which tells us that we have debug equal to true.
-
1:11
We also have a database which gives us our database configuration for our local.
-
1:17
Now notice the difference here is that inside database for
-
1:21
the production environment, you'll see default is set to MySQL.
-
1:25
Here it's not.
-
1:27
So, because it's not set, it means
-
1:29
that it inherits initially from this database file.
-
1:33
So what we want to do is change our MySQL settings for local.
-
1:38
So we'll say driver is MySQL.
-
1:40
The local host is fine.
-
1:42
But our database is not homestead.
-
1:44
Our database will actually be odot.
-
1:47
Now our user name will be homestead and our password will be secret.
-
1:51
So now all we have to do is save this file.
-
1:54
Let's switch back over to our routes and show what database we're currently using.
-
1:58
So head to your routes file.
-
2:01
And now I'm going to paste in a route with a closure.
-
2:04
A very simple route to get /db.
-
2:07
Just as a temporary URL.
-
2:09
And then we're going to do a return DB select.
-
2:12
So this is just running a method on the
-
2:14
db class which is an internal Laravel db class.
-
2:17
And then we're saying we wanna do a select database statement.
-
2:21
Which is just going to give us our current database.
-
2:24
Let's run that now.
-
2:26
So here we have laravel.div/8000, which is going to give us our main link, main list.
-
2:32
Now we're gonna do /db, hit enter and it
-
2:35
will show us that our current database is odot.
-
2:37
This let's us know a couple of things.
-
2:39
One, we are connecting to our database, so if
-
2:41
we were, for instance, to modify our configuration here.
-
2:45
Two, mess this up so let's say our database is foo, and
-
2:50
then go back and refresh, we're gonna end up getting an error.
-
2:53
PDOException, which is unknown database foo.
-
2:56
So, that would be something you would get
-
2:58
if you were not connecting to your database.
-
3:01
Let's change that back to odot and then hit refresh.
-
3:05
And so now you see we're connecting, we're making
-
3:07
our route, and we're tying into the db class.
-
3:10
And running a select statement to find out what database we're currently on.
-
3:14
In this case our current database is odot, so, we're good to go
You need to sign up for Treehouse in order to download course files.
Sign up