*/ public function toArray(Request $request): array { list ($actions, $actionGroupObjects) = $this->widthActionObjects(); $items = []; foreach ($this->resource->items() as $item) { $object = null; if ($item->object_type == NotificationObjectType::ACTION->value) { $action = $actions[$item->object_id]; $objectAction = $item->extra_fields['action'] ?? $action->action; $object = [ 'id' => $action->id, 'action' => $objectAction, 'action_label' => __(sprintf("action-labels.label.%s", $objectAction)), 'created_by' => new UserProfileResource($action->createdBy), 'comment' => $action->commtent, 'object_type' => $action->object_type, 'object' => [ 'id' => $action->object_id, 'name' => data_get($actionGroupObjects, sprintf("%s.%s", $action->object_type, $action->object_id)), ] ]; } $row = [ 'id' => $item->id, 'object_type' => $item->object_type, 'created_at'=>(string)$item->created_at, 'content' => $item->content, 'read_at' => (string)$item->read_at, 'read_status' => (bool)$item->read_at, 'display_id'=>$item->display_id, 'object' => $object, ]; $items[] = $row; } return $items; } protected function widthActionObjects(): array { $actionIDS = collect($this->resource->items())->where("object_type", NotificationObjectType::ACTION->value)->pluck("object_id"); $actions = Action::with(['createdBy'])->whereIn("id", $actionIDS->toArray())->get(); $actionGroupObjects = []; foreach($actions->groupBy("object_type") as $objectType => $items) { $approvalObjectType = ApprovalObjectType::from($objectType); $actionGroupObjects[$objectType] = $approvalObjectType ->modelBuilder() ->whereIn("id", array_column($items->toArray(), "object_id")) ->pluck($approvalObjectType->nameField(), "id"); }; return [$actions->keyBy("id"), $actionGroupObjects]; } }