Laravel 5.8 – From The Ground Up
Lesson 37 of 52 · Laravel 5.8 Tutorial From Scratch – e37 – Model Factories · Education
Laravel 5.8 Tutorial From Scratch – e37 – Model Factories
welcome back in today's lessons we're gonna start to tackle model factories and something called database seders as the name would suggest a model Factory
simply is a representation of fake data that gets created for a specific model the example of that would be a lot of times you just need some fake data in your database to be able to make your
designs work to make your development easier and all of that up until this point we've gone into the database and added that manually or had to create customers we had to create companies all
of that from scratch but throughout development you really don't care to have any real data but rather it would be easy to have a single command for us to fill our database with data so that's
what we're gonna do today now letter will actually ships with a model factory for the user so let's take a look at that first and then let's create our own so let's pull up the user
factory class and here it is this is what a model Factory looks like so this is a factory defined for a user and then we are assigning each of the fields to a particular data so let's go through
those very quickly if we pull up the create user stable migration we see that we have of course an ID a name and email a timestamp for verified ID and a password remember all of this ships with
laravel so we did not create any of this this is the very first time that we've actually seen this migration so we have all of these fields and then we have corresponding fakir data for each of
those fields now without getting too deep into faker faker is actually a PHP package that ships with laravel to help us generate fake data of all sorts so we have a name in which we are creating a
fake name we have an email where we're creating a unique safe email meaning it doesn't have any of the special characters and then we're creating a verified at timestamp using them now
which is just a helper function for carbon and then we're assigning a password notice here this is actually a string little not a faker data so it will always equal to password that way
were able to log in as that user and then as a remember token we just have a random string ten characters long okay so how would we run this Factory so to run a factory currently let's bump into
tinker PHP artisan tinker and inside of this the first thing I want to do is I want to show you what I actually have in the database so I can show you how we're gonna change it so let's say a user
fetch me all of my users but go ahead and plug their name all I want to see is the name of our users so now we see we have two users inside our database we have test user and new user all right so
now let's see how we can use this Factory to whip up more users so to use a factory let's use the factory helper function and then inside of there we have to give it the path to the class
that we're trying to create a factory for so in our case of course we know it's the user class so we need to do back slash app back slash user colon colon class and then as a second
argument you can pass a number we'll check out out in just a second but we're just going to tag on another call to the create method so what are we doing here we are creating some users using a
factory and then we're gonna persist them to the database now if you wanted three users for example you could actually just put as a second argument a three so comma three this will create
three users if you leave that blank and you don't pass in a second argument like so then you only create one user let's do that now so there we go looks like I created one user let's go back a couple
of clicks and pluck all of the names of our users and now we see that we have three users notice up here we only had the first two and now we've added this new doctor miles or is this coming from
again that's coming from faker faker is faking all this data for us so it's giving us random strings to use let's add another one and this time let's maybe create three of them and then
let's run that command one more time where we plucked all of the names and sure enough we have a bunch of users inside our database that's pretty cool now let's create our own and what I'm
thinking is we've had these companies and we've had to manually create these companies wouldn't it be easier for us to have a factory for the company I think so
so let's check out the command PHP artisan and let's go up to make factory so make factory let's do PHP artisan help make factory and so we see that it takes an argument
of name and then we can pass in a model the model that we're trying to create okay PHP artisan make factory and then obviously as a model we want the company
model oh it says I'm missing the name of course let's back up and let's do PHP artisan make factory and let's just call it company factory of course we need a
name for our actual class and then the model is going to be company all right so that's the full command there there we go let's check out PHP storm and if we scroll up inside this database
directory which we have not explored yet and this is the first time we're going to be exploring it inside this database there is a factories directory and inside factories we do have company
factory cool so we've created our own factory so now we need to define the fields the easiest way for this is just to pull up the company migration so let's pull up companies migration and
all we need is a phone and a name alright let's do that now so let's say a name and we know that we can use this faker for a name and then we need a phone so let's pull up faker again and
let's get a phone number so we have a name and a phone number so now we can generate companies as well so now for name this will actually give us a name of a person however if you said company
that'll give us the name of a company something that would really be more company appropriate and that's it our factory is good to go let's see what we have to do to actually use this factory
let's go back to PHP artisan thinker and let's see what we have in the company database first so company all and I want to pluck their name that way we get a list of names of companies and
all we have is this ABC company okay let's whip up a new company using a factory so factory backslash app backslash company : : class and then it's called the create method on it and
sure enough we have a company now all right let's run that company pluck one more time and now we have two companies and if we switch over to Chrome and try to create a new customer we do see we
have that company there let's create 10 companies now how will we do that remember as a second argument you can do 10 and now we've created 10 companies let's see what that looks like
there we go look at all these companies we have here so we can easily generate random data for testing purposes int for development purposes very very quickly so this is very cool now alongside with
model factories we're going to talk about database Cedar's so in the next episode we're going to create a database seeder so we can create an entire world for our application in a single command
stay tuned
Model factories offer a very convenient way to add test data to our database during the development phase. Let’s create a factory for our Company’s table now.
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!