Subscribe to PHP Freaks RSS

Mahmoud Zalt: Eloquent Relationships Cheat Sheet

syndicated from www.phpdeveloper.org on October 6, 2017

Mahmoud Zalt has a recent post to his Medium.com site that shares a cheat sheet for Eloquent relationships you can use with the Eloquent database layer in a Laravel application.

He goes through each of the relationship types and provides both a diagram showing the relationship and some sample code to make it work:

  • One to One
  • One to Many
  • Polymorphic One to Many Relationship
  • Many to Many Relationship
  • Polymorphic Many to Many Relationship

The post ends with a combined table of all of the relationships, how they connect with the other models, the number of tables involved and how to set values to a related model.