Laravel 5.8 – From The Ground Up
Lesson 38 of 52 · Laravel 5.8 Tutorial From Scratch – e38 – Database & Table Seeders · Education
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 https://www.coderstape.com
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
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!