0

I am currently learning Laravel, and I have a query regarding the best practice for creating entity relationships whilst writing a migration as I am confused as to how best do this.

Lets say you are writing an entity schema that you know will have a Many to Many relationship or a One to Many relationship or even a foreign key, etc.

Are you meant to code the relationship into the migration itself, or after it?

All the tutorials I've seen up to now seem to gloss over how to make the migration know that Entity A will have relationship with Entity B and put the relationships in the models using hasMany(), etc

This confuses me, are you meant to put the relationship structure into the migration; or is it better to create your migration and then make your relationship in the model?

Thanks now

zardon
  • 301
  • 1
  • 2
  • 9
  • Basically you migrate your tables and define your relationships in the models. for eg I have author_id in posts table and use belongsTo in my Posts table as author() function. – ytsejam Mar 25 '14 at 12:24
  • I'd say foreign keys in your migrations are not necessary but they are good practice. If you are using the Eloquent methods for building relationships, you probably don't have to worry too much about it. http://stackoverflow.com/questions/1629537/sql-server-foreign-key-constraint-benefits – user1669496 Mar 25 '14 at 13:07

0 Answers0