stefancoding's avatar

Can I use relationship feature in Laravel without using migration?

Can I use relationship feature if I just create table in database manualy, then set in model the table name?

0 likes
5 replies
dani94's avatar

Migrations are only to help you manage your database. Models matching tables, it doesnt matter who and how this tables are created.

3 likes
Snapey's avatar

Yes you can but its not good practice.

Good practice is to describe the database in code so that it can be recreated at any time and you can apply changes in a controlled manner to other copies such as production.

If you inherit a database, you can run php artisan schema:dump against an existing database and this will create a baseline of all the existing tables. You can then add migrations from that point on.

https://laravel.com/docs/8.x/migrations#squashing-migrations

stefancoding's avatar

@Snapey Yes, I know that the best solution is to describe the database in code but I don't wanna really use it because I am working on an online shop where need to add some relationships. That shop has 358 tables with millions of products so I don't wanna accidentally delete some data from the database.

Snapey's avatar

I didn't suggest deleting anything....

Please or to participate in this conversation.