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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭