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

rszabo@crowleyauto.net's avatar

Access data from collection from multiple tables.

I have a collection that produces the data I want, but I can't figure out how to access the user data of the collection. I have tried a bunch of things but I can't access the name property.

                    @foreach($peopleyouorg as $people)
                                  {{$people->id}}
                {{$people->UserName()->name}}
                        @endforeach

In the controller
    $peopleyouorg = Organizer::with('UserName')->whereHas('UserName',function($query){
        $query->where('user_id',\Auth::user()->jumperid);})->get();

json return

{
"id": 3,
"created_at": null,
"updated_at": null,
"user_id": "SZRO-279",
"member_id": "1409",
"status": null,
"user_name": {
"id": 3,
"name": "Bryce Bainter",
"email": "[email protected]",
"uspa": "237113",
"jumperid": "1409",
"jmid": "2813",
"created_at": "2018-01-12 22:41:51",
"updated_at": "2018-01-12 22:41:51"
}
},
{
"id": 4,
"created_at": null,
"updated_at": null,
"user_id": "SZRO-279",
"member_id": "4529",
"status": null,
"user_name": {
"id": 4,
"name": "Kenyatta Barton",
"email": "[email protected]",
"uspa": "543617",
"jumperid": "4529",
"jmid": null,
"created_at": "2018-01-15 20:40:45",
"updated_at": "2018-01-15 20:40:45"
}
},

The model

public function UserName(){

return $this->hasOne('App\User','jumperid','member_id');

}

Sorry, never posted here before.

0 likes
3 replies
Snapey's avatar

Can you edit your post please and put ``` on a line before code and the same after. We might then see what the problem is

Cronix's avatar

Looks like just $people->userName->name

Please or to participate in this conversation.