This is caused by the way Eloquent guesses the foreign key for belongsTo
, which is methodName_id
by default. So in your case it was responsible_id
, while calling:
$agreement->responsible_id;
returns null
, so you would end up with WHERE id IN ()
.
Now, Eloquent by default doesn't let empty array for WHERE id IN (...)
so it does not throw any errors from the DB. To achieve this it passes array(0)
instead, and this is what happened there.
Ah, that does make a lot of sense and is a bit obvious when I think about it. I thought it used the bound class ("User") to guess the name of the ID field, not the method name.
I started out with belongsToMany() relationships - those seems to guess the "class_id" format on id's though (and disregard the method name), so I assumed belongsTo() worked in the same manner.
Anyway, thank you for the clarification, I appreciate you taking the time to explain :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community