在Laravel / PHP中将一个额外的字段推入一个数组中

I am returning project team member details.

The tables involved are:

 users
 projects 
 members

(edited for brevity they look like this)

'users'
    ('id',
    'name',
    'email',
    'avatar',
    'avatar_type',
    'status')

'projects'
    ('id',
    'name',
    'description')

'members'
    ('id',
    'user_id',
    'position',
    'capacity')

Projects can have multiple members and the members table is effectively a pivot table for the users.

Currently using this:

$projectTeam = [];
$project[0]->members()
    ->with('users')
    ->where('user_id','!=',0)
    ->where('status','=',"Active")
    ->get()
    ->each(function ($member) use(&$projectTeam)
    {
        $projectTeam[$member->id] = $member->users;
    });

What I would like to do in the loop above is to evaluate the user record and then if the avatar field is populated and the avatar type is a certain value create/push/append another field into the results so that I can add in extra information.

Something like this:

$projectTeam = [];
$project[0]->members()
    ->with('users')
    ->where('status','=',"Active")
    ->get()
    ->each(function ($member) use(&$projectTeam)
    {
        $projectTeam[$member->id] = $member->users;

            if($member->users->avatar<>"")
            {
                if($member->users->avatar-type=="1")
                {
                    array_push($projectTeam[$member->id], "imgClass='alpha'")
                }
                if($member->users->avatar-type=="2")
                {
                    array_push($projectTeam[$member->id], "imgClass='beta'")
                }
            }
    });

So that when I call those image records on the front end I can just populate the html class with that new field.

Something like this:

<img src="{{$teammember->avatar}}" class="{{$teammember->imgClass" /> 

Hope this makes sense!

How do I go about adding in that extra field?

How about:

$projectTeam[$member->id]->imgClass = 'alpha'

And now for the real Eloquent answer...

What you want to do is do

class ProjectTeam
{
    protected $appends = ['imageClass'];

    public function getImageClassAttribute()
    {
        return 'whatever';
    }
}

Now whenever you call $projectTeam->imageClass, you'll get whatever.