Hey, my book is finally available. Find it here.  Eloquent Beyond The Basics!

hasOne and belongsTo confuse me!

Feb 5, 2022

This title comes from a Reddit post I saw the other day. The poster argues that the hasOne() and belongsTo() model methods are "counter-intuitive".

My first instinct was to think he was wrong. But the more I thought about it the more I remembered that I have been confused by these relationships many times.

A rule of thumb

The post also mentioned a rule of thumb that might be helpful for anyone reading.

The class that has the foreign key uses belongsTo()

A cheatsheet

Seems about right for me, but I think I'll still get confused sometimes. That's why I made a simple cheatsheet to help me remember which is which. I also added many to many relationships since I know when I just started I always forgot where the foreign keys were supposed to go in those as well.

Download it below 👇