doujingqu3030 2018-03-06 21:53
浏览 37
已采纳

在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?

  • 写回答

2条回答 默认 最新

  • dongtuhe0506 2018-03-06 22:31
    关注

    How about:

    $projectTeam[$member->id]->imgClass = 'alpha'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器