Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arctushar's avatar

hasManyThrough problem

My three tables are as below

materials
    id - integer
    name - string

estimates
    id - integer
    material_id - integer
    quantity- number

mprs
    id - integer
    estimate_id - integer
    quantity- number

Material model is

class Material extends Model
{
    public function mprs()
    {
        return $this->hasManyThrough('App\Mpr', 'App\Estimate');
    }   
}

When controller use below code, it show result

$abc= Material::all();
    return $abc;

but when in controller I use

$abc= Material::all();
    return $abc->mprs;

error is is below

Property [mprs] does not exist on this collection instance.
0 likes
1 reply
EventFellows's avatar

the relation is for ONE instance and not for a collection.

This should work.

return $abc->first()->mprs;

Alternatively you can loop through your collection with foreach.

2 likes

Please or to participate in this conversation.