Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e38 – Database & Table Seeders

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

Laravel 5.8 Tutorial From Scratch – e38 – Database & Table Seeders

we're making great progress in our application and learning laravel so we're picking right back up if you missed the first part of this this is

part 2 where we're going to be talking about database ceders in the previous lesson we put together a model factory for our companies and created some fake data for application now I want to work

on creating an entire world worth of data for the application in a single command and for that we're gonna use database seders so let's explore database seders now if we go back to our

application we have this seeds directory inside database and this database cedar as it ships doesn't do anything as you can see it only has one method and the one call to this users table cedar is

commented off so it's really not doing anything at all so what can we do in here inside the run method you can run anything you need to do to create stuff for your application and buy stuff I

mean users I mean companies anything that your application is gonna need for development purposes now this is great because a lot of times you're gonna have to erase your entire database and

generate all of this data again just because you had to make a change to a migration so it's great to have model factories and cedars to help you just put some data back in the database so

you can continue development and that's what a database cedar will do so let's check out this command i will uncomment this command and let's try to get this command working as it ships out of the

box it actually doesn't work and I'm going to show you how to run this cedar let's head back to the terminal and let's take a look at PHP artisan if we scroll up a little bit we have this DB

colon seed this command seats the database with records that sounds like exactly what we need to do so PHP artisan DB colon seat and we get an error like I said this command doesn't

actually work when it ships with laravel but we can make this command work so what it's saying is that it cannot find this users table cedar class so how can we create a seeder let's

scroll up and with every level thing that we have of course we do have a seed or command so make cedar of course generates a cedar for us so let's do that now

PHP artisan make a cedar and the name of the cedar is gonna be user's table cedar okay cedar created successfully let's go back to phpstorm and now in our database

directory we do have this users table cedar okay so this class in this class basically look exactly identical but the difference is that this database cedar

think of it as the orchestrator of this entire operation this is the main file and then we're going to have some smaller files for each of the tables that you need to fill so in this case

this is related to the users table now as we learn before we have that factory right we learned this in the previous episode so in a factory we can whip up a user right we know we can actually whip

up several users if we do as a second argument we can put three for example to create several users and then we need to run the create method on it so this is the exact same thing we did in the

previous episode through tinker except now we're putting it in a class so we have factory app user class and we're going to whip up three users great let me jump to table plus just to show you

what our database currently has so we have these users right here right that's all we have now let's run that DB seed command one more time so PHP artisan DB colon seed and this time it doesn't fail

this time it does work and let's go back to table plus and let's check out our table so there we are we have three additional users to our table okay what if we wanted to add companies for

example automatically right now we have these companies but when I run DB seed I also want to add some companies to the database so how would we do that well

the whole sequence goes something like this PHP artisan make me a cedar in the name of the cedar will be company's table cedar now the name and convention of this is just

simply to use the plural and then table cedar now it looks like I missed an s here so there we go company's table cedar that would be the naming convention of this truthfully you

can name it whatever you'd like but you're gonna end up with a lot of files that start with user a lot of files that start with company and it's just easier if you actually specify what that class

is in the name just makes it easier to browse through your files so let's add that company cedar right now and let's go back to phpstorm and now we have this company's table

seer now since we already added a company factory in the previous episode we can simply just use factory to whip up some companies so that's app company and let's do ten companies create and

now remember we have to go back to the original class this database cedar class and add that call method to that new company cedar otherwise it doesn't work remember this is the file that

orchestrates everything database cedar class sits atop of all of your individual cedars so let's say company's table cedar class and now when we run that DB seed again we're gonna get a

call to the users table cedar and a call to the company's table cedar let's try that now PHP artisan DB seed and sure enough we called the users table and the company's table and if we go back to

table plus and we hit refresh there we are we have some more companies and of course we do have some more users that we didn't have before again the reason

being is that we're generating all of this automatically alright so let's do one final one for our customers let's quickly create a factory and then a cedar for our customers how can we do

that let's jump back to the terminal and let's say PHP artisan make me a factory customer factory and I also want a cedar so PHP artisan make me a cedar for customers table cedar

all right so now we have the two things together now we just need to set up our factory and then set up our cedar let's do that now back to phpstorm let's take a look at our customers

factory first and so of course it's empty as always let's take a look at the migration so create customers table migration and it looks like we need a company ID it looks like we need a name

and email and an active column alright so now let's handle this company ID first so this company ID is really related to another company right we need to be able to generate a company

alongside with a customer you can't have a customer if you don't have a company interesting so how do we tackle that so that's actually fairly easy let's jump back to the customers factory and let's

say that company ID is and what can we possibly put this can you put one yes you could put one but the problem is that if company ID one doesn't exist meaning the company doesn't exist in the

database this will fail so what can we do instead instead we can actually put a call to our factory right from a factory you can create a factory using a factory so we'll say factory create me a company

why not of course we can do that so create that and then what we actually need is the ID but laravel will take care of that for us in the background which is pretty cool

the one thing I missed was that I forgot to pass the model name into the create factory method so we can simply just say app customer in here so that way it is related to the correct model all right

what else do we need we need a name an email and an active column all right so name email and we need an active column ok easy enough so for name let's use faker again so fake

her name now for email we can use exactly the same thing that the user factory used which is this unique safe email so let's copy that over and bring it in

so that's faker unique safe email and then finally for active we could just give it a 1 that way it defaults to an active user if we need to override this weekend and

it's actually fairly simple all right so we have our customer factory ready to go so the next thing we need to do is tackle the factory cedar and here it is so customers not whoops it looks like I

forgot the s in customers no problem let's rename that file now to customers refactor and there we go all right no big deal so what do we need to do here well here same thing we need to whip up

a factory for our customers class and then we need to run the create method on it now I am importing customers here at the top I've done it both ways either one will work if you want to avoid this

extra line up here then of course you can inline the way we've been doing and say app customer and then you don't need that import at the top either one will work all right so one final step is of

course we need to tell our database cedar to please call that new customers table cedar so customers table cedar and there we are I'm gonna hit safe and what I want to do is I want to delete the

entire database and start from scratch so to do that we can run PHP artisan migrate fresh and that's it everything is empty let's go to table plus hit refresh do we have any companies no

customers no nothing we don't have anything at all our database is totally totally blank and if we go to our actual project you see that we can't even log in we don't have anything at all so now

we can run PHP artisan DB seed so it looks like it can't find my customers table cedar the reason for this is probably because composer did not detect that we rename that file no problem if

you ever get a weird error like that we can run composer dump – auto load and that should fix it let's run the command again PHP artisan DB C and now we get a different error

it says the class company is not found I must have missed an import here somewhere yep sure enough so it looks like here we're using company but we did not fully

complete the path so say app company and that should fix it all right let's try one last time and let's see what's going on here ah there it is so we have the wrong character I

bet you saw that let me fix that really quick and here this uh-huh there we go all right one final final time and let's check it out PHP artisan DBC and finally here we go

we are working let's jump back to table plus hit refresh and sure enough we have our users we have our customers and companies perfect so obviously we will put all of this together in two commands

right we ran PHP artisan fresh and then we ran our cedar but as it turns out there's even a shortcut for that and we can run PHP artisan migrate fresh – – seed what this will do is give us a

brand-new fresh database and then seed it for us and there it is you see the migrations and then you see our Cedars so single command and we have an entire world for our application using levels

table Cedars

A database seeder is comprised of all of the necessary code to generate a world’s worth of data for our app. A table seeder is an individual seeder for a particular table. Learn all about both of us, in this lesson.

For the best experience, follow along in our interactive school at

DigitalOcean Referral

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