laravel项目 取数据时去除重复字段查询

    //获取全部数据
    $Activity = $Activity->paginate($pagesize);
        //临时数组
        $name = array();//存放user_id
        foreach ($Activity as $k => $v){ //数组循环
            $PersonnelModel = new PersonnelModel(); //实例化模型
            if (!array_key_exists($v['user_id'],$name)){ //user_id 是否存在临时数组
                //不存在临时数组
                $PersonnelModel = $PersonnelModel->where('personnel_id',$v['user_id'])->select(['personnel_name'])->get()->toArray();
                $Activity[$k]['publisher'] =  $PersonnelModel[0]['personnel_name'];//操作人
                $name[$v['user_id']] = $Activity[$k]['publisher']; //赋值给name临时数组
            }else{
                //存在于临时数组直接获取
                $Activity[$k]['publisher'] = $name[$v['user_id']];
            }
        }

 

你可能感兴趣的:(laravel)