Noob Noob - 1 month ago 8
PHP Question

It only shows the first data. How can I show every data with the same id?

This code only shows the first data it finds.

public function getGroupInfo($eg_id){
$members = \App\EventGroupMembers::where('eg_id', '=', $eg_id)->get();
if ($members->count() > 0) {
$user = \App\User::find($members[0]->user_id);
echo $user->first_name;
} else {
return false;
}
}


Table event_group_members

eg_id user_id
1 1
2 2
2 3
3 4
3 5


Table user

user_id first_name
1 tony
2 steve
3 bruce
4 thor
5 clint


What should I do to show all the first_names with the same eg_id?

Answer

Try this:

foreach($members as $member)
{
    echo $member->id;
    // It will echo diff id for each iteration
}

Explanation: $member is an Std class object that have more than one value, if you are using this:

$members[0]->user_id;

means you are referring its 0th index value. To access all value you have to use loop.

Comments