Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e07 – SQLite Database

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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

Course Source Code

Hit us up on Twitter with any questions or comments @codertape (

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!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue