Dear everyone,
I hope to make this clear and to get some help to resolve my issue.
I have 3 tables.
A, B e C. We can see C as a pivot table of A and B (A has many C records, B has many C records but exists, if any, one and only one record for a given A.id and B.id)
Now, how can I get, in a relation where I m querying A records, the related C records that belong to B?
So basically going from an HasMany, Relationship (Record of A hasMany records of C) to an HasOne relationship (from all the records of C belonging to each single record of A, I want only the ones where b_id = B.id)?
I m trying to get all the features of an HasOne relationship (especially eager loading) without the need to map all the A records to filter out the ones not wanted.
I was thinking of a function
// Model/A
return $this->Cs->where ('B_id',B->id)->first()
Or setting the attribute like
Attribute::make(set:FN(C $c)=>$this->Cs->where ('B_id',B->id)->first())
Both of the solutions don't work because 1. Eager loading not possible
2. Need to map all the results AFTER querying the A models
Is there a solution for this?
Ideally it s really simple, but it seems that no parameters can be passed to the HasMany To get an hasOne relationship
Ps: latest or oldest are useless, as the records related don't depend on dates .