since Eloquent supports relationships, would you recommend to additionally define foreign key constraints in the migrations? Why?
Thanks
since Eloquent supports relationships, would you recommend to additionally define foreign key constraints in the migrations? Why?
Thanks
Eloquent supports relationships, but it doesn't enforce them - that has to be done in the database itself. Databases also have a habit of outliving code. What happens when you want to use this database with some other ORM in the future?
Defining foreign key constraints is generally considered "good design", regardless of how you end up consuming that database.
Sure i would recommend you additionally define foreign key constraints for only one reason.
Laravel does not support cascade delete out of the box. This makes handling of cascading deletion of deep related records a pain.
It is indeed considered good design and I would recommend implementing them yourself in Eloquent using a BaseModel
class and bootable traits for such.
Nowadays, Eloquent has robust reactive event based mechanisms which must be kept in the know of such constraints and updates to properly reflect them throughout the system.
Common use cases would be an ACL audit system, database recovery, cache sync or bindings update mechanisms etc.