7 months ago

Understanding models/migrations in Laravel v. C# VS Entityframework

Posted 7 months ago by dwlangham

I'm brand new to Laravel with a background in VisualStudio, C#, MVC and EntityFramework. Something I'm having to get my head around is the difference in how the two approaches handle model definition and migrations.

Here are my questions, followed by context: Without looking at the database, how would I know the properties and data types of a model? Yes, I could examine all the migrations, but that's not a good answer (there might not be migrations). And more important: What is there that insures that changes in the data model don't break my code?

In C#/MVC/EF, migrations are generated from a class definition. You specify properties and decorators in the model class and run a terminal command to generate a migration, and then update the database (applying the migration). Rarely do you need ever edit a migration directly.

In Laravel, it's just the opposite. You create a migration that updates the database, but the model class need not have any properties defined.

This difference in workflow and model definition points to a more fundamental difference. So far as I can tell, your Laravel code need never specify the model properties. In other words, you can skip the migrations and just edit the database tables directly in Mysql, and then create a class in Laravel using the correct naming convention and voila, you have a working data model. In C#/Vs world, you have to have a class defining your models, and even if you're working with an existing database you still need to run a migration that essentially matches your model class with the existing tables.

What's more, if your data model changes in C#/VS--for example, you change the data type of a model property--VS will complain that changes to your model haven't been applied to the database. In essence, the data model class acts as an interface (or contract) that insures that your database and your data model (and the code written to use it) remain consistent.

What is there in Laravel (or 3d party or IDE products) that does this? Or am I missing something?

Please sign in or create an account to participate in this conversation.