Back

Hey Laravel Developers

When defining relationships on our models, we often follow the convention of naming the relationship similar to the related table name.

For example, if we have a "Car" model, we define the "user_id" column on the "cars" table while defining the "users" relationship, which represents the owner relation.

However, I prefer naming columns based on the domain for which I am building the application.

For instance, in this scenario, instead of naming the column "user_id", I would name it "owner_id" and define it as the "owner" relationship.

To me, this approach feels more fluent and readable.

What do you think? Let me know your thoughts!

image

1

58

Literally have `vehicles` table with `user_id` column and `owner` relationship. Relationship is `owner`, since it is self-explanatory in the code ( `Vehicle::with('owner')->...` ), however `user_id` is self-explanatory in the DB (what are foreign keys for)

1

154

In response to @HungryBus

So it depends on whether you aim for making the db clear or the code clear.

Thats a tradeoff you have to decide for.

76

  • No matching results...
  • Searching...

/ 1000