Laravel 5.8 – From The Ground Up
Lesson 7 of 52 · Laravel 5.8 Tutorial From Scratch – e07 – SQLite Database · Education
Laravel 5.8 Tutorial From Scratch – e07 – SQLite Database
welcome back another essential element for any website is being able to persist some data and of course we do this through a database now if you haven't
worked with any frameworks before you may not be familiar with the fact that typically frameworks will allow you to use many different databases for example you may use my sequel in your production
server but you may use SQLite for development or maybe Postgres or any other supported database so in this episode I want to get you set up with SQLite SQLite is an SQL like database
that is actually file based and requires very little configuration to get you started there's a lot to cover in this episode so let's get right to it so the first thing I want to do is I want to
show you a new file if you scroll down one of the files that are actually sitting in the root directory is this dot env file and if we open that file you'll get the idea that this is the
file responsible for setting up things like your database your mail once we start sending email maybe your AWS your Amazon or pusher it's a lot of information on here we're not gonna go
into a lot of detail in this file just yet we're really only interested in this one line right here DB underscore connection it defaults to MySQL but like I said I want to use
SQLite so we're gonna replace that with SQLite and then we actually need to erase the rest of these DB lines so let's get rid of that and just leave DB underscore connection equals SQLite okay
now in our terminal we actually need to create this file that SQLite is going to use to put our data into and so to do that we can run touch and then we're going to dive into the database
directory and then we need to create a file called database dot SQLite and we hit enter and when we do all it does is create an empty file inside the database directory called database SQLite now a
new concept that I'm going to bring you to is migrations now migrations are the fix for some of the problems that you may encounter whenever you're trying to have a local environment a production
environment and maybe even a middle staging environment and that is that if you add say a column or a table or you make a change to one of your tables you have to go in and manually change this
in every database instance that you are running so that's how we fix that migrations modify your database in a way now migrations by concept only run one time and you only run migrations forward
meaning that in production you don't typically rollback is the term that is used which means that you're gonna undoing a migration if you need to revert a change you would actually
create a new migration which reverse your database back to what it was this concept of migration is something that's gonna take a little while to get used to but just know this migrations are your
friend and they are an integral part of being able to work in a team so the next command that we're gonna run is the migrate command so let's say PHP artisan migrate whoops I made a type of PHP
artisan migrate and there we go and so we see that we created a user's table and we created a passwords resets table not sure what that is just yet don't worry about it for now but just know
that label actually ships with two migrations already and so that's what just ran what I want to do is start fetching our customers list from our database instead of hard-coded let me
show you what I have right now if we go back to the customers controller we have this hard-coded list of customers but of course I want those to come from the database as you typically would so back
in my terminal I need to create something called a model a model represents a single row in your database and it is responsible for saving updating deleting or doing anything that
you need for that particular row so let's create a model so if I have a table of customers my model is a customer right singular not plural so to make that model we're going to use a new
PHP artisan make command so let me run PHP artisan and let's go back to that make section and here's the one that we're looking for make model so the make model
creates a new eloquent model class okay let's run that command PHP artisan help make model and so now we see that this command requires a name but we also see that if you pass the flag – M then we
get this migration file that I've been talking about so this creates a new migration file for the model so I do want that so the full command is going to be PHP artisan make model customer
singular – M so this is gonna tell laravel to make me a model for customers and in the process go ahead and give me a migration as well and typically models and migrations go hand-in-hand you'll
have a model and you'll have to create a table to hold that data so let me go ahead and create that and so now we see that we've successfully created a model and a migration back in phpstorm I can
find this new migration so let's scroll up I'm gonna close this app and we're actually looking for database it's a new directory in your route and then inside database we have migrations and then the
last one here we see creates customers table and if we double click that we see a class that basically describes this customers table now so far there's not much in here except we see that we have
an ID column and we have these timestamps don't worry about the timestamps for now we'll touch on that later on so what if I wanted to have a customer's name okay well let's go ahead
and add table arrow string and we'll just call it name and that's good enough for now I'm gonna hit save and then I'm gonna head back to my terminal so now we need
to migrate our database right we've added a new migration so I need to migrate right now my database only has the default tables and it doesn't have this
customer table in it PHP artisan migrate and now we see that we are migrated and we have this customers table added to our database okay so now how do we generate
some data inside of that table well for that we're going to use another level trick which is an awesome tool called tinker and you do that by running PHP artisan tinker and tinker is a shell
which basically runs PHP you can write any PHP command in here and it will run it as if you had written it in a script it's very cool we have this customer model and let's just say that I want to
fetch all of the data inside of it so you would say customer : : all parentheses and of course we get back an empty array there are no customers inside just there so let's create a new
customer so I'm gonna say customer equals new customer and then let's set the name to John Doe hit enter okay and then we need to persist it so customer safe and save that record and it returns
true so now we have a customer in our database let's run customer all one more time and now we do see that we have one entry where before we got an empty array now we have one customer called John Doe
okay fair enough let's go back to our controller so I'm back in customer controller and I want to replace this data with that one customer
so we'll say my customers are gonna come from customer so say customer : : all and so now we have a list of customers now if I want to preview what we're actually getting back from this I can
add DD as a function and then let's pass in customers let's go back to the browser and check out what we have if we hit refresh then of course we see that we have one customer and we see here in
attributes that their name is John Doe so we're looking for this named variable at this point okay let's go back to phpstorm let's get rid of this and I bet you everything's gonna blow up now
yeah not quite what we had in mind so now we are getting a big list of items and we are no longer getting exactly what we needed easiest way to fix this is just a target
the name right that's the only thing that we are interested in now because we're using a database now of course you have things like ID and you have these created and updated at time stamps which
were not interested in right now so we're targeting this name so let's change our view so let's go back to my customer view customer that blade that PHP and so now we're not just targeting
customer but I want the customers name let's hit refresh and there we are so now we have one customer coming from our database let's add another one new customer and then let's call this new
customer let's add a name of Jane Doe and let's save that customer save and that equals to true let's go back to the browser hit refresh so there we are we are fetching two customers from our
database now we covered a lot of ground in this episode so if you didn't catch all of it the first time around make sure to rewatch it a couple of times these types of concepts of
migrations and models and database are difficult concepts to understand so don't feel frustrated just keep rewatching and when you're ready let's move on to something else
Let’s get the basics of working with a database in Laravel by setting up an SQLite database from scratch. Let’s also create a migration and a model for our customers.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!