Laravel 5+,将多个相同的集合聚集在一个数组中
Laravel 5+, gathering more than one of the same collection in an array
所以我有一个问题。我正在尝试收集用户 'Log' 信息(在此上下文中登录意味着维护问题或相关问题)。现在,每个日志集合 returned 都基于他们是哪种类型的用户。一个用户也可以是不止一种,比如租户,也可以是系统下的第三方。所以我需要 return 所有日志,即使它们有多种用户类型。
这是我的代码:
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;
}
这个问题是,如果他们是多个用户类型,则输出是数组与每个条目分开:
=> [
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: [],
},
]
显然数组的第二部分没有数据,但您得到的是 returned 的格式。这在遍历数组时会导致问题。我只是想知道我是否可以 return 将所有日志信息放入数组中的相同索引 [0] 中? (然后我可以 return $log[0])。
谢谢
您可以对集合使用 merge()
方法。
在您当前返回日志的地方尝试以下代码;
$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;
所以我有一个问题。我正在尝试收集用户 'Log' 信息(在此上下文中登录意味着维护问题或相关问题)。现在,每个日志集合 returned 都基于他们是哪种类型的用户。一个用户也可以是不止一种,比如租户,也可以是系统下的第三方。所以我需要 return 所有日志,即使它们有多种用户类型。 这是我的代码:
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;
}
这个问题是,如果他们是多个用户类型,则输出是数组与每个条目分开:
=> [
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: [],
},
]
显然数组的第二部分没有数据,但您得到的是 returned 的格式。这在遍历数组时会导致问题。我只是想知道我是否可以 return 将所有日志信息放入数组中的相同索引 [0] 中? (然后我可以 return $log[0])。
谢谢
您可以对集合使用 merge()
方法。
在您当前返回日志的地方尝试以下代码;
$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;