Model Factories | Laravel 8 For Beginners | Learn Laravel 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dari and i hope that you have a wonderful day now that we have set up our database connection and went over the basics we need to input data inside our database and if we hop to mysql we can do this by writing down our own queries inside the command line so let's say that we want to insert into posts the tables title and body and the values are this is my first post and the value of the body is body of the first post since we have set our id to auto increment we don't need to increase it automatically inside the query let's hit enter you can see that one row has been affected let's write down select all from posts and you can see that we have one row inside our table right now and don't look at the created data and updated that that will come later on my issue is that this took way too long and this is for only one post what we could do is to use our model factories which is basically a pattern for creating fake entries for our database tables so it's just adding dummy data inside our tables based on the data types that we've passed in inside our migration by default every factory that we have is named after an eloquent class but you could also name them after the table if you're not working with eloquent so let's hop to the code because i want to show you where our factories are stored so right inside our database folder we have a factories folder so let's open it by default you could see one factory which is the user factory so let's open it right here you can see that there is a default user factory class which extends the main factory class so let's click through on the factory class let's see what's going on over here in the factory class you can find a lot of properties and static factory methods patterns that we could use let's scroll down because right here you could see a factory instance of a given number we could create a new factory instance we could get the raw attributes and way more things that we could do let's see well if i scroll down we have more than 700 lines and since i can't cover them all i just want you to take a minute and scroll through them to see the power of factories and if you're done let's continue on with this video by going back to our users factory all right let me open it again we have a function where we're defining our user model factory we're returning an array right here with a couple of fields and this is an associative array with the name of name email email verified add password and remember token and these are not randomly added right here since we want to insert dummy data inside the database the keys of this array need to be the same as the column names so what we can do is to go to i term let's desk users and right here you can see the id name email email verified ad password and the other stuff so if you go to the visual studio code we're basically adding everything that's right here inside the database now the values that we are trying to set so this faker name is faker unique are created with a faker property that we have which is the faker php library that allows us to generate random data for testing what i would like to do right now is to generate a new factory class so let's hop to the command line let me open a new tab and right here i want to use a command called php artisan and we want to make something again which is a factory called post factory all right let's hit enter our factory has been created let's hop to visual studio code and let's open our post factory which has the same structure as our user factory we're pulling in the post model and we're creating a property called model which is equal to the class of post but let's say that you don't want to use this specific model that we have so the post model you don't want to change it manually by setting it equal to let's say user for that we could use another command but before we perform it let's remove this file for a second now that's up to the terminal once again and in here let's set the arrow up and let's add a flag to it of double dash model is equal to post we could also set it equal to user but for now let's just set it equal to post because that's what we need all right let's save it let's hop back to visual studio code again we have our post factory again let's keep it easy for ourselves let's go to the users factory and last copy the entire return array now let's replace it inside of our post factory alright we don't need our remember token for a post and we also don't need our password all right let's change the name to the title let's set it equal to this faker but not the name but title let's change the email to body and let's get rid of unique safe email and let's set it equal to a paragraph what do we have next we could also add the created underscore at which is equal to now but with the release of laravel 8 we need to change a couple things right here we need to use another import called illuminate backslash support backslash sdr and instead of saying post call and call and class we need to write down backslash app backslash models backslash so we need to write down the entire path to the post model and don't forget to add the first backslash in front of app otherwise it won't start at the root directory and that's what we need now i want to use php artisan thinker right now and i won't go in depth about it because i want to make a complete video about it later on but for now just follow along we could also use seeding but that's not what i prefer to use because i think php artisan thinker is way stronger so let's save it let's hop to our command line let's write down php artisan thinker and this will basically redirect us to a shell where we could perform a lot of stuff what we could do right here is to set our entire path to the model so app backslash models backslash post then double colon because we want to call the factory global helper so let's write down factory and then we want to create a new row so pointer create let's hit enter and right now you can see that we created one row with dummy data right here we have a title we have a body the created updated at and we have our auto increment id we could also add more than one row so let's hit the arrow up again and right after factory we could call another method which has the name of count and inside the count parentheses we need to specify the amount of rows that we want to add so let's say two hit enter and we created two more posts before i wrap up the video let's go to my sequel and in here let's say select all from posts and you can see that we have added three new rows inside our post table just as easy as that this was it for this video about factory models if you do like my video and you want to see more or you just want to support the channel just click on the subscribe button down below and don't forget to like the video
Info
Channel: Code With Dary
Views: 8,329
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel full course, php laravel youtube, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, laravel tutorials from scratch to advanced, model factories, model factories laravel 8
Id: Lsc2gIPSMyI
Channel Id: undefined
Length: 8min 2sec (482 seconds)
Published: Fri Dec 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.