Bromira's avatar

Self Referencing Relationship?

Hi all,

Thanks in advance for any help & advice.

I'm building an API and one endpoint is to return information about a "call" (job) to a user with the relationships "appointments" and "call_actions".

If "call_actions" doesn't exist, I want to attempt to find the previous "call" and get it's call_actions relationship so the consumer can use it to pre-fill a form.

Unfortunately whenever I try I get "InvalidArgumentException".

Could anyone please advise/point me in the right direction?

Thank you!

Model (SCCall.php)

Controller

0 likes
2 replies
Snapey's avatar

I dont know if this is the whole problem, but using with will always result in call_actions existing, but being empty

if (!$call->call_actions) {

try

if (empty($call->call_actions)) {
1 like
elk_dev's avatar

Try using

if(!$call->call_actions->count()) {

instead of

if(!$call->call_actions) {

call_actions will never be null, because it is a collection, so your condition should instead check if the number of elements in call_actions is null.

Please or to participate in this conversation.