Can I use eloquent
in migration? The thing is, I need to add some values to the database, based on the relations I have set in the model. I know I could do it by query builder
style with some SQL or somehow but it would be easier to just use eloquent. I am not sure if it won't appear as a mistake in the future. So, what to use in migration - eloquent or query builder?
Laravel Migrations allow developers to programmatically create, update, and destroy database tables, working as a version control system for your database schema.
A migration class contains two methods: up and down . The up method is used to add new tables, columns, or indexes to your database, while the down method should simply reverse the operations performed by the up method.
Advantages and Disadvantages of Laravel MigrationWeb applications can be built on it quickly. It is easy to learn. The documentation is easily available. Fulfils almost every criterion demanded by modern web application build up.
php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command. php artisan migrate:refresh is a two in one command that executes the :rollback command and the migrate command.
Can I use
eloquent
in migration?
Yes, but it can be a bad idea.
Eloquent relies on your models to be in sync with your database. If there’s a difference, you may find that Eloquent struggles. Bear in mind that the migrations are specifically to deal with database queries, rather than any kind of object-driven data that you might get from them.
I would say as a rule, always stick to using DB
in migrations. But there is nothing phyiscally stopping you from using Eloquent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With