devondahon's avatar

Relationship model not recognized

After querying an item $part with its relationship records, I want to log it but the relationship records is recognized as a standad collection instead of its own model Record. How should I fix it ?

        $part= Part::with('records')->find($id);
        logger($part->records->map(fn(Record $r) => $r->amount));
0 likes
4 replies
krisi_gjika's avatar

$participation->records is a list of App\\Models\\Record::class, since the relationship is set up as one to many or many to many.

krisi_gjika's avatar

@devondahon not sure what the mapping is trying to do. If you intend for $part->records to return a single App\\Models\\Record::class you need to check the "records" relationship declaration in App\\Models\\Part::class, in that case it should be "record" not "records" and it should return a HasOne or BelongsTo relation.

PovilasKorop's avatar

@devondahon I have a guess that your method records() is a hasone or a belongsto after all, could you check in the model?

Or paste full code of the Part model here?

I've seen people multiple times in the past mixing singular/plural method names and getting errors later.

Please or to participate in this conversation.