#Strange issue where HasOne returns null in a resource but is ok in tinker

33 messages · Page 1 of 1 (latest)

ashen spindle
#

I'm trying to get this hasOne to work, and it does in tinker, but not on the request from postman.

    {
        return $this->hasMany(PitchForm::class);
    }

    public function primaryForm(): HasOne
    {
        return $this->forms()->where('language_code', $this->primary_language)->one();
    }

These are the relations on the "pitch"

    {
        return $this->belongsTo(Pitch::class);
    }```
Inverse on the pitchForm
``` 'form' => $this->when($isPitch, PitchFormResource::make($this->whenLoaded('primaryForm'))),```

This returns null always...
```Pitch::find(5)->primaryForm``` But this in tinker returns the form... What am I doing wrong in the resource??? I have tried a million things and nothing seems to change it. I have dd'ed the query, which is correct, the bindings are correct, but the result is null, the bindings are language_code = 'en-EN' and pitch_id = 5 in this instance... I have tried copy pasting the query to table plus and again it works as expected....
sterile lily
#

Not to mention, the relation would also need to be loaded (as your resource code shows), it wouldn't be loaded by default

ashen spindle
#

I am loadMissing on the resource, I didn't include it as it was "a given" 🙂 But how come the relation works fine in tinker then?

sterile lily
#

Probably because it's not a relation?

ashen spindle
#

How do you mean it isn't a proper relation? It's building from another relation, that I included above it.

sterile lily
#

Because that's not how you'd to one of many relations, as I just linked

ashen spindle
#

TDeclaringModel>one()
Convert the relationship to a "has one" relationship.

#

that's what I'm doing

#

on a hasMany to a HasOne

#

If it wasn't a proper relationship laravel would issue an error since i use the return type

sterile lily
#

Ok, well then it's all correct and the docs are wrong I guess

ashen spindle
#

I sense some attitude? If you aren't in the mood to be kind and help then please refrain from responding

#

I am genuinely asking for help, not snarky comments

#

Even tinker suggests this is correct

sterile lily
#

I'm telling you to take the approach that's mentioned in the docs. You're facing an issue, you're not following what's mentioned in the docs, then you blame me for giving you bad advice. And seeing as the issue you're facing is that it's not detecting a relation properly, I'd say that's the issue. You're the one being quite stubborn here. You could just simply try the approach mentioned in the docs, if that doesn't work, we'd go further debugging. I'm trying to help you, but you're the one not wanting to be helped.

ashen spindle
#

Again this attitude, if you look at the screenshots, you can see why I may be confused, as they show in tinker that the relation is detected, and correct result is returned... So why wouldn't it in the resource when using the EXACT same logic??

#

I am trying your suggested approach as we speak, but I really don't appreciate the shortness

#

Weirdly the approach suggested returns null

sterile lily
#

Just because one thing works, doesn't mean it's correct. Yes, you're seeing a result, that does not mean the relation is triggered and loaded properly. I'm genuinely trying to help you, but you're being extremely stubborn and somehow blaming me for that. Anyway, good luck, I'm not in the mood for helping ungrateful people that don't want to be helped.

ashen spindle
#

return $this->forms()->one()->ofMany([
'id' => 'max'
])->where('language_code', $this->primary_language);

#

this returns null

#

I followed this

#

"Just because one thing works, doesn't mean it's correct." Fair point, but strange statement, in the sense, it should work...

#

If it works, it should be correct

#

If I remove the ofMany() since ->one() converts it to a has one, then it works, but keeping the ofMany returns null

#

"select * from "pitch_forms" where "pitch_forms"."pitch_id" = 5 and "pitch_forms"."pitch_id" is not null and "language_code" = 'en-EN' and "pitch_forms"."deleted_at" is null"

#

Pitch::find(5)->primaryForm()->toRawSql()

#

this is the result from the toRawSql, again it confounds me why it isn't working elsewhere

ashen spindle
#

So after a good amount of googling and digging, the reason it works in tinker is because the pitch is loaded, so the primary language exists.. Whereas in the relation definition the primary_language is not loaded... So how do i use an attribute from the pitch model in a where clause on the hasOne relation??