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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?