Introducing Factory Generator0:00
Now, yet another new addition to Laravel 5.5 is a new model factory generator. So if we just take a look at this, and we take a look, there's a new make:factory command. So here's the difference. In the past, in your database/factories folder, you would have a single factories file, and this would house all of your model factories. And if you're not familiar with model factories, they're like the blueprints for your models. So you can populate them with some fake data, and then quickly whip up 30 random User models for your tests or for your local development. But yeah, that single file could very quickly just get long and confusing. So now they can be split up according to the model.
Generating Video Factory0:34
But yeah, that single file could very quickly just get long and confusing. So now they can be split up according to the model. So let's take a look. php artisan make:factory, and I'm still working with this concept of videos, so we'll call it Video. Make a factory for my Video model. So if we switch over, now we have a new file here. And we have a good step here to get started. So here I could say, well, the title should be faker, just give me a sentence. And then the archived status should be faker, and give me a random boolean.
Seeding Data with Tinker1:00
So here I could say, well, the title should be faker, just give me a sentence. And then the archived status should be faker, and give me a random Boolean. And that's it. We're done now. So if we boot up php, artisan, tinker, I can now say, well, I would like a factory and specifically 10 of them. So let's whip those up and persist them. And we're all set to go. So now if I were to say, app, video, give me all of them, sure enough, we have 10 random items in the database.
Benefits of Split Factories1:23
So now if I were to say, app, video, give me all of them, sure enough, we have 10 random items in the database. And yeah, now in Laravel 5.5, we split the factories into their own files, and you now have a generator to quickly whip this up. Because if you were anything like me, you'd always end up doing junk like this, where you'd say, app, series, in my case, and then you would just copy and paste there. So this is a much cleaner and quick solution.
