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