Search code examples
databaselaraveleloquentmigrationseed

Why we use laravel seed, if there is migration and eloquent that plays the role for database concept?


I confused about laravel seed concept. Let me clear, in laravel there is the eloquent and migrations that we use for CRUD operation for any database that then we use in our controllers. In seed also uses for the storing information in the database table. So why is it important instead of we have those two eloquent and migration for database.


Solution

  • Migrations are nothing more than Laravel's way of maintaining a database in a friendly way. Exporting and importing .sql files on every team member's computer everytime someone makes a change to the database gets old and annoying really fast. Migrations make sure you only have to type a artisan migrate and you're up to date.

    Eloquent is Laravel's "Object Relational Mapper" which defines how your app communicates with your database (in this case a Model since Laravel uses MVC architecture). Models in Laravel are written in php and don't do direct actions on your database, they act as some sort of facade that makes it really easy to do stuff in your database.

    Seeds are little files that quickly let you push something in your database, so you can test your app. For example, if you need 3 different users with different user roles, running a command artisan db:seed is a lot faster than making all those users manually in SQL commands.

    Hope it's a bit more clear for you!