Laravel 6 Beginner
Lección 7 de 14 · Laravel 6 Beginner – e7 – MySQL Database & SQLite · Educación
Laravel 6 Beginner – e7 – MySQL Database & SQLite
in this episode we're going to be handling databases [Music] one thing about laravel that you need to
know is the fact that you can actually use any database that you would like and they are interchangeable that's because we actually use a layer of label called eloquent eloquent is a database layer
and what it does is it actually encapsulates all of the different services and make them all have the same API which means that we can develop in one particular database for example
MySQL and then we can deploy on our actual service in a completely different database for example we could even use Amazon AWS at that point but our code doesn't change in development most of
the time I actually use SQLite it's actually a flat file database I'll show you how to set one up in this episode it's very simple to do but you may use MySQL so again you don't have to change
any of your code it just works and that's one of the beauties of using a framework like levo so let's start with MySQL definitely one of the most popular options now MySQL for Windows if you
need to install it you can simply do a quick search for MySQL for Windows and you can go to the downloads panel right here and at the time of the recording the current version is 8.0 17 you can
download the appropriate file for your system and you can also download 5.7 now 5.7 has been around for a long time and it is in most servers today I don't think most servers have upgraded to 8
and if you want to keep your local development the same as your production server I would recommend you actually install 5.7 so either one would work now if you are on the Mac then we can bring
that in through homebrew if you haven't installed homebrew in your Mac just yet you simply copy this command into the terminal now once you've got it installed you basically get access to
the MySQL command so if you typed in MySQL in my case it actually says access denied because it doesn't seem to find a user typically you'll do something like MySQL – you and then the user is
route and that does give you access to MySQL once you are inside this command it's very simple to do you will simply create a new database and let's just give it a name of laravel and with a
semicolon and now that's it we've got a database set up in our system very simple to do now if you are over in the Mac I do recommend you download sequel Pro it's a hundred percent free and it's
a nice way for you to do this as a GUI let me pull that open right now and here it is so if I connect to my localhost again localhost is just root and typically the password is not set again
this is all based on your system so you may need to figure out how to get this working in your system opening up localhost let's take a look at my available databases we see that I
have this new laravel database and this is the one that we just created so that's it we've got the setup done so let's jump back into phpstorm and now let's visit a brand new file
that we haven't talked about and that is the environment file the environment file is in charge of setting up the application in its current environment and what that means is that your
application is going to use slightly different services in production as it would in your local development one perfect example of that is the actual database layer that we are using in our
case right now we're using MySQL but as I mentioned before you could use any number of services that file sits right at the root of your project and it is called dot e and V if we pop open dot
env we see this section right here with DB underscore these are all of your database settings right now it is set up to MySQL and it is set up to one 2700 that one that is localhost
right now our database is set up to level and the username is set to root so when I created that laravel database in my local environment I kind of already knew that the settings were going to be
like this by default now if you created a database and called it something else in that case that would change here and if you do have a password go ahead and add it right here now one tip here you
may need as if your password has special characters or spaces or something like that you can always add it inside quotes and then you can type in whatever you want at that
so keep that in mind whenever you're setting up your database all right so now that we've done all of this setup it's time for us to talk about what we actually want to handle in this episode
if we take a look at the actual services page that we currently have we've got two services but again as a quick review if you check out the hello controller it's just coming from this array right
here that we created what I actually want to do is create a database and I want to pull our services from the database so by the end of this episode we'll have that tackle there's a couple
of more things that we need to talk about and that is models now if you remember levo is an MVC framework and the M stands for models models are just a representation of your database tables
now a single model a single instance of a model represents a single row in your database and the models as a whole is the entire table so if that is true then that means that we need a services model
because we're trying to fetch services and our table name is going to match that pay close attention to the naming convention because if you do level will take care of a lot of stuff for you
behind the scenes if you decide to name things against the convention of laravel be warned that you're going to have to do a little bit more work than necessary I strongly recommend that as you're
starting out you always follow the naming convention the naming convention for level is as follows your model is going to be the singular version of the word in our case it would be service
right it is singular not services then your table is actually going to be plural so that case our table name is going to be service ists with an S at the end now notice that this is
uppercase and this is lowercase so we're going to need a service model and we're going to need a service table now to create a table typically you would actually probably go into sequel pro or
some other MySQL program and actually generate the table but there's a very important concept to understand here and it is the concept of migrations don't get overwhelmed by migrations Mike
relations are just a simple file that describes how the table should be created in the database as opposed to you doing it manually we're actually writing a small script that generates
the table this makes it very easy for anyone to add the exact same table to their project and very easy for you to deploy a project to production and create this database on the fly now
we're going to use a couple of different PHP artisan commands so let's jump back in the terminal let me actually exit out of this MySQL let's clear out the screen and let's bring up PHP artisan let's
check out the make section remember we already touched up on make controller now I'm interested in a model and I'm interested in a migration and of course we do have a make model and we have a
make migration let's start with the model if you remember from the previous episode you can always run PHP artisan help and then the command name so say make model this will give you the manual
for this particular command with all of his usages options and any other important information the only thing that we are interested in is the name so let's go ahead and run that command PHP
artisan make model and remember from the naming convention the service is going to be capital S service and it's going to be singular so we'll go ahead and create that model and then we do need a
migration so let's do the same thing again so we'll say PHP artisan help make migration and check this out we now have a name but we can also go ahead and pass in this create flag this is the one that
I'm interested in because of course we are creating a table so let's go ahead and run that command will say PHP artisan make migration and what do we call this let's go ahead and say it
creates services table and notice the underscores so we'll say something like creates then the table name and then just table then we do need to pass in this – – creates and then we need to set
that equal to the table now we're going to be creating so – – creates and then let's go ahead and name our table services remember naming convention is very
the table is all of your services but the model is one service it's one row in your table that's why your model is singular and your table is actually plural we'll go ahead and run that
command and we are good to go so what we just did was we generated two files in our project let's take a look I'm going to close everything up so you can see exactly where they are right
inside your app directory we now have a service that PHP file this is your model and if we open this up check this out we've got a class with the name of service and it extends model that's how
you know that this is a model and remember that key word I told you about that you've probably forgot the name of already that is eloquent notice how model it's a database eloquent model
remember eloquent is that layer that database layer inside of laravel that is extremely powerful you're going to learn to love eloquent the other file that we created is going to be inside our
database directory inside migrations again remember the migrations are just a script that generates a table think of it as the exact same steps that you've taken yourself manually to create a
table inside a MySQL database but now we're doing it through scripts and we're doing it through code that way we could put it in source control and it could be replicated very easily
now notice down here there are a couple of other migrations already in this project we'll go ahead and ignore those those actually come with level we'll touch up on what they are in a later
episode but notice the last one down here creates services table great let's open this up so check this out this class has two methods an up method and it's got a down method one thing to know
about migrations is that they run forwards and back so when you run them forwards meaning up you perform a specific action and then if you run them backwards that would be the down method
you do the opposite so in this case we are creating a database in our up method and in our down method we are actually dropping it we are removing it so we are doing the opposite of what it does it
undos the command now one thing is down methods are not required so you can ignore them all together so how do we generate this table let's get right to it
the first thing we'll notice here is that yes we are creating a table by the name of services and we've got an incremental ID column for us already as well as timestamps and this is all great
news however for our example of services we're going to need some place to put the actual service name so let's create another one of those here and we'll say a new column inside my table it needs to
be a string let's go ahead and save a string for name and that's going to pertain to my service name okay so far so good we need to run one more command because
we actually need to run this file this file doesn't exist yet let me jump back to sequel probe notice that there are no tables right here even if i refresh there are no tables inside my database
my database doesn't know about any of these migrations there is a command for that so let's run PHP artisan one more time and let's check out my great so there's a whole migrate section and
if you run PHP artisan migrate let's actually run the help command first will say PHP artisan help migrate this is what runs the database migrations that sounds like exactly what we want let's
go ahead and run that command PHP artisan migrate and now we get a list of tables like I said there are some migrations in there that we actually didn't create we're really only
interested in this creates services table let's go back to the GUI and let me open this up if i refresh notice now we've got these tables also notice that I have this services right here that's
the table that we just created and as a matter of fact it also has that name the ID and they created and updated at those came from the time stamps that we had right here in our migration so the time
stamps are right here and the big increment is right here now the reason why laravel includes this is because you're going to want to have unique IDs for all of your entries and it's always
great to have time stamps when ever a record is created or updated and that's these two rows right here level creates those for us out of the box because it's such a common thing and
that's why they're there now let's go ahead and add some services here let's say cool service one and then let's go ahead and say now for those two and let's create another service and we'll
also go ahead and initialize those all right so we've got some stuff inside of our database now I want to actually display it right here notice how I've got these two services again just as a
quick refresher those are being hard-coded in from my hello controller that is certainly not what I want to do how do we fetch something well we use the model this one right here this
service is model so let's go ahead and replace this and I want to fetch them from my database I want these two right here all right let's check this out let's go back to phpstorm
and then let's go ahead and create a new variable here and we'll say services equals and then we need to actually do the full name space so we'll say app service and then we can simply grab all
of them give me all of my services and now one cool trick we can do here is we can use a function called DD it's very useful what it does is it just outputs out whatever you pass in here and it
stops all operations so let's go ahead and die and dump the services and now when we visit the browser and I hit refresh it actually says this site can't be reached that's because I'm not
running PHP artisan surf PHP artisan surf there we go I had turned it off to create the sequel database and if I hit refresh there we go so notice we get back on array and
it's got two services in it and if we keep opening this up inside the attributes section this first one is called cool service one and then on the second one if we open up attributes
again we have another service so just like that we actually pulled from our database now what I need to do is change up the code my services are no longer going to be hard-coded so I can delete
those lines I can delete this dye and dump as well and now we are passing in services the same string as well but now we're grabbing them using our model the way
that these actually work is that they are objects so I will no longer be able to just simply grab services one trick I can show you here is why don't we actually just check out one of these
services so we can see what we're getting back we could do the same thing here with the dye and dump as well but this time let's go ahead and dine dump just a
single service remember we are inside a for each loop so this will actually dine dump after the very first service let's check that on the browser hit refresh and sure enough I get a single service
notice that this is not a collection or an array it's just a single service and if I want the name then I can use simple object notation so I could say service arrow name and now I get cool service
exactly what I need great let me delete that line and instead of just service I need to do service arrow name hit refresh and sure enough we are grabbing it from the
database let's add a third one here just for example will say example laravel let's put my time stamps again and if I hit refresh boom there we go how cool is that very very easy to do
very cool now one last thing stick with me for a couple of more minutes I want to show you how to set up a completely different database and that is SQLite let's jump back into my code and all we
would need to do here is actually create a blank file inside this database directory so I will create a new file here and let's call it database dot SQLite and if you do that now this is
just going to be a simple text file completely blank and again notice the path it is inside the databases directory and then it's called database SQLite so now let's use a completely
different database service just to prove how easy it is to change that so in my DMV file instead of MySQL let's use sq light and then I will erase all of the other lines SQLite is a very fast and
easy to do database because it's a file by us simply creating this file right here we have a full-blown database let's jump back into the terminal I will stop my PHP artisan surf but this is a
completely fresh database so we actually need to migrate it because remember it's completely blank right now so say PHP artisan migrate and sure enough it created all these tables for us but now
if we jump back into here and hit refresh whoops let me run that command again PHP artisan surf there we go let's try that again no services available so just like that
we changed from one database service which was MySQL to another one which was SQLite it's just that simple now if you wanted to add I'm gonna show you a cool trick we're not quite ready to talk
about tinker but let me show you a pretty cool tool that ships with level and it is tinker so we'll say PHP artisan tinker and this allows you to interact with your application through
the command line it's pretty cool so say service equals so let's create a new record in that database we could do that using the model so we'll say new app service and now we have an instance
of service so I can simply set the service name equal to hello from SQLite and then let's go ahead and save that service so we'll say service Arrow safe okay so now let me go ahead and serve my
application one more time I'll hit refresh and there we go so hello from SQLite you want to change the database again no problem let's go ahead and change it back to MySQL and just like
that now we are running under MySQL so that is the crash course on models migrations and everything to do with MySQL and SQLite now there are many different database services that you can
use with laravel so just as a very last thing we can actually take a look at what services are available for us if we take a look inside the config file this is this directory right here it's
config and then there's a database dot PHP and if you scroll down a little bit we'll notice here that we have some different connections so we use the SQLite connection and we use the MySQL
connection but also notice that we have a PG SQL we also have SQL Server and there's many many other ones as a matter of fact you can even create your own driver so keep that in mind that's going
to wrap it up for our episode today go ahead and create some tables in your project and play around with migrations and models and get comfortable with that and when you're ready let's move on to
the next episode
Let’s get the basics of working with a database in Laravel by setting up a MySQL database from scratch. Let’s also create a migration and a model for our services. To wrap it up, let’s change to a SQLite database to show how simple it is.
For the best experience, follow along in our interactive school at https://www.coderstape.com
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
About This Course
If you have been looking to learn Laravel, there hasn’t been a better time. The framework reached an important point in its life, with the release of Laravel 6. There has never been more demand for Laravel developers than now. In this series, we are going to get you started in your path to becoming a Laravel Artisan. Let’s get right to it!