Laravel 9 Eloquent Factory And DB Seeder - Laravel 9 Testing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign [Music] [Applause] you about database Factory and the cedar in level 9 so without wasting any time let's get started I have already installed laravel on my local machine if you want to know how you can install reliable 9 on your local you can watch a video given in the description so let's jump into the code let's open a code so first we need to understand what is Factory in variable so Factory is nothing but a representation of the model with some fake data very often there is a use case in every application where we require some fake data to be tested while you know doing a unit testing or while doing large number of data testing so instead of inserting a data through database query or you know manually in level 9 we have factories which can be used as a tool to insert large number of data with a single command let's see how we can use this Factory so if you go into your folders open this database folder you'll see there is a factories folder and the user Factory is given as an example how you can write your factory so I'm just going to delete this and we will look into this from initial so let's delete this file move it to trash Now by running a command we can create this Factory so to create a factory I am going to use this Factory for users model to insert 1000 records in a user's table so what we can do to create a factory you need to run a command PHP artisan make Factory is the command and the name of factory I am just giving it as user Factory and you can specify the model for which you are creating this so I'm just creating it for user model so this will create our Factory and then assign that factory to the user model let's create this as soon as you run this command your factory has been created let's open it will go inside database factories folder let's open this as soon as you open this you will see it is actually extending this Factory class if you see in our models it has this user model so it is going to extend that class right so let's in the definition we need to Define all our users property like what is the user's fillable properties so let's go into the user model let's open this if you see in the user model we are extending this trade which is has Factory so this by extending this has Factory trade this allows us to create a free data on behalf of users so we are using these fields like name username mobile number email and password and there will be remember token and email verified ad so we are going to use the minimal what we can do so let's go ahead and name this as a name property and in the factory we are going to use the faker library of PHP to create a fake data so what we'll do we'll just simply Define this figure and then when you extend this you will have different function available right so I'm just giving it a name function so it will return a fake name every time you can also specify your gender if you wanted to I'm not going to do that the next property we have is the email so for email what I'm going to do again I'm going to use this Faker library and create a fake email so for that I'm just using this figure the email should be unique in the property so I'm just using this unique property and after that I just want it to be this email to received so I'm just using this saved email function so this will give me a unique safe email every time so the next property which we'll have is a username right so again username will have their unique username so we'll use this Faker library and see if there is any function for username yes there is so we are using this username before that I have to use a uni for this as well so this will be our unique property and then the username all right the next property which we have is mobile number so I'm going to Define that mobile number and then again with the help of Faker Library I'm going to use it for a free mobile number right so instead of mobile number it will be a phone number yeah so there you go you get a phone number right the next thing which we have is a password for password what I'm going to use I am going to use the facade which is available in laravel hash make and this will make a hashed string of password and the password I'm just giving it password so every for every user the password will be password but it will be hashed so the next property which we have is email verified ad so let's look at the property email verified ads so what I'm going to do for email verified at I'm just going to use now so whenever that this whenever this Factor will be used the current timestamp will be assigned the next is remember token so I'm just copying this field name from here and using the same field name because we are extending the same model so what I'm going to do I'm just going to use this string facade and using this length or the random string of length 10 so this will make sure every time we have a random string for this we need to import this from support so we have to use this here and similar for hash as well we have to use it here otherwise it will give you error so as you see we have defined all the properties of that user model let's save this now in order to use this you can run you can use this Factory as a command so let me open the terminal and in the terminal what I can use is basically create a record in database so before creating this I just go into the database we have two users now let's open this in the terminal and write down PHP Artisan Tinker in order to run the command directly now I can run these command directly um models user and then Factory I'm using this Factory class and create this should create a single record in our database let's hit as you see it has created a record this particular information printed here so let's go and refresh our database you see there is a record which has been created on behalf of this user model what if we had to create 50 out of I mean at least 10 let's get created this 10 record at once so all we need to do after this Factory we give account which whatever count you'll give I'm just giving it a 10 it is going to create that number of record for that particular Factory so let's enter this you see it has created 10 record at the same time now if you refresh this you see the 10 record has been created again and everything is there right so it's really easy if you want to create thousands of record in a single command so Factory is the best solution which laravel gives us to create this now every time you don't have to use this command or the Tinker command to create the data so all you need to do is let's exit this first and let's close this terminal now what we can use instead of using this command in terminal we can use cedar cedar is nothing but a laravel tool which we can use to seed our databases so basically whenever we have these series whenever we wanted to see any database we just run this single command and that will seed our database so in order to create that you have to run a command so again let's open the terminal to create a seeder you just need to run a command PHP RP sync make seed and the name of Cedar right so I'm just giving it user table Cedar correct so let's hit this as soon as you hit this command it will create a cedar file inside your database serial folder let's open up this you will see this is it actually extending the cedar trade and inside the Run whatever you are going to use that it will run this command so I just wanted to seed thousand records by running this series so what I'm going to do that I simply use this user model and then Factory using this Factory I'm just giving this account of 1000 right and then I'll just write it as create what it will do so if we are using user so you we need to import this class so this will be app models and user so here you need to on the top you need to import this user model class in order to use this here in the Run function all right so we have created our seeded now how we wanted to use this you can run this particular seizure command by running a command PHP artisan and then DB seed it will run all the serial but if you want to specify this user table seater come on so you can pass a class name the class name will be the same what we have used to create a Serial right so it will be user table seater right so as soon as you run this command you'll see your record thousand record for this user table will be created as you can see database reading completed successfully now if you go into the database refresh you see there is thousands of record has been created in the users table imagine if you are doing it manually it will going to take a lot of your time but using this Factory and Cedar it will be as easy as within a second you'll get all those recorded in your table so I hope this will give you Clarity how you can use factories and how you can use cedar to seed your database tables within singles command and that will create thousands of record for you there's a lot of other things which you can explore inside a factory inside a seeder through the laravel documentation I hope this video is informative for you if you like this video please do subscribe our Channel if you face any issue while using database reader or a factory please do let us know in the comment section we'll try to get that resolved for you till the next video keep watching keep learning thank you for watching [Music]
Info
Channel: TechTool India
Views: 2,701
Rating: undefined out of 5
Keywords: laravel, laravel 9, laravel factory, laravel seeder, laravel 8, laravel tutorial, laravel 9 tutorial, laravel seeders, laravel model factory, laravel factory and seeder, laravel factories, factory seeder laravel 9 testing, seeder, laravel factory faker, laravel 5.5 factory, laravel 8 tutorial, laravel seeder factory, laravel for beginners, factory seeder in laravel 5, factories and seeders in laravel, laravel 9 seeder, laravel 8 seeds, seeder en laravel 9
Id: dROeStI4uvI
Channel Id: undefined
Length: 15min 25sec (925 seconds)
Published: Sat Nov 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.