Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Model Factory and a DB seeder in Laravel?

What is the difference between Model Factory and a DB seeder in Laravel?

like image 425
Kabil Avatar asked May 03 '17 12:05

Kabil


People also ask

What is model factory in Laravel?

July 28th, 2022. Laravel Model factories are one of the best features you can use in your application when it comes to testing. They provide a way to define data that is predictable and easy to replicate so that your tests are consistent and controlled. Let's start with a simple example.

What is database seeder in Laravel?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

How do you call a factory in a seeder?

we need to create create factory class with following command. * The name of the factory's corresponding model. * Define the model's default state. Now we are ready to run seeder.

What is the use of factory in Laravel 8?

Defining Model Factories Instead of manually specifying the value of each column when you create this test data, Laravel allows you to define a set of default attributes for each of your Eloquent models using model factories. * Define the model's default state.


1 Answers

I Prefer to view Seeders and Factories from another perspective.

As Mentioned by others, Model Factories are used for testing purposes and populating your database with a huge amount of fake data. This could be used for unit testing and performance testing of your system.

On the other hand, Seeder Classes are used for inserting data that is crucial and important for the system to operate with. Something like the superadminstrator, Basic Roles/Role permissions in the system, Countries, Cities and other data that may not come from CRUD's

like image 124
Rami Amro Ahmed Avatar answered Sep 29 '22 09:09

Rami Amro Ahmed