Noob Noob - 1 year ago 65
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 Source

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:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download