Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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 :)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ErikEklund erikeklund Joined 14 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.