drq22639 2018-04-08 19:32
浏览 64
已采纳

Laravel 5+,在阵列中收集多个相同的集合

so I have an issue. I am trying to collect a users 'Log' information (Log in this context means a maintenance issue or something related). Now each collection of logs returned is based on which type of user they are. A user can also be more than 1 type, for instance a tenant, but also a third party under the system. So I need to return all logs even if they have multiple user types. Here is my code:

    public function returnLogs() {

    if($this->isPosidaciousAdmin()) {

        //All logs
        return Log::all()->latest();

    } else {        

        //coalesce logs for each user type the user is

        if($this->isStaff()) {

            //Logs created under working agency ID and log assignee logs
            $logs[] = Log::where('agency_id', $this->activeAgencyId())
                        ->orWhere('created_by', $this->user_id)
                        ->latest()
                        ->get();

        } 

        if($this->isThirdParty()) {

            //Log Assignees logs

            //Get all assigned log IDs
            $assignedLogs = LogAssignee::select('log_id')->where('user_id', $this->user_id);

            //Find all logs with assigned log IDs
            $logs[] = Log::whereIn('log_id', $assignedLogs)->latest()->get();

        } 

        if($this->isTenant()) {

            //Logs at current property and logs created by tenant
            //If tenant currently has a tenancy
            if($this->currentProperty() !== null) {
                $logs[] = Log::where('created_by', $this->user_id)
                            ->orWhere('property_id', $this->currentProperty()->property_id)  
                            ->latest()               
                            ->get();
            } else {
                $logs[] = null;
            }
        }
    }

    return $logs;
}

The problem with this is that the output if they are more than one user type is that the array is split with each entry:

=> [
 Illuminate\Database\Eloquent\Collection {#829
   all: [
     App\Log {#830
       log_id: 1,
       log_title: "Test Log",
       log_type: "Maintenance",
       log_severity: "Normal",
       log_status: "Open",
       agency_id: 1,
       property_id: 1,
       created_by: 4,
       created_at: "2018-04-08 19:05:54",
       updated_at: "2018-04-08 20:07:48",
       deleted_at: null,
     },
   ],
 },
 Illuminate\Database\Eloquent\Collection {#837
   all: [],
 },

]

Obviously the second part of the array has no data but you get the format which is being returned. This is causing issues when looping over the array. I'm just wondering if I can return all log information into the same index[0] in the array? (I could then return $log[0]).

Thanks

  • 写回答

1条回答 默认 最新

  • dongqing7789 2018-04-08 19:40
    关注

    You can use the merge() method on the collection.

    Try the following code where you're currently returning logs;

    $mergedLogs = $logs[0];
    
    // although arrays begin at 0 we've already got the first one
    for ($i = 1; $i < count($logs); $i++) {
        $mergedLogs = $mergedLogs->merge($logs[$i]);
    }
    
    return $mergedLogs;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀