Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e37 – Model Factories

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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

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