Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent in migration

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?

like image 498
Epsilon47 Avatar asked Feb 19 '18 14:02

Epsilon47


People also ask

What is a Laravel migration?

Laravel Migrations allow developers to programmatically create, update, and destroy database tables, working as a version control system for your database schema.

What methods are used in database migration classes?

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.

What is the advantage of Laravel migrations?

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.

What is php artisan migrate?

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.


1 Answers

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.

like image 177
Zoe Edwards Avatar answered Sep 20 '22 15:09

Zoe Edwards