TaskExport.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Exports;
  3. use App\Models\Task;
  4. use Maatwebsite\Excel\Concerns\FromCollection;
  5. use Maatwebsite\Excel\Concerns\WithHeadings;
  6. class TaskExport implements FromCollection, WithHeadings
  7. {
  8. private static $index = 1;
  9. public function collection()
  10. {
  11. $tasks = collect([]);
  12. Task::query()
  13. ->where("parent_id", 0)
  14. ->with(['children', 'assignTo', 'createdBy', 'parent', 'requirement', 'project'])
  15. ->filter(request()->all())
  16. ->allowed()
  17. ->orderBy("id")
  18. ->chunkById(200, function ($items) use (&$tasks) {
  19. foreach ($items as $item) {
  20. $tasks->push($this->formatRow($item));
  21. if ($item->children) {
  22. foreach ($item->children as $child) {
  23. $tasks->push($this->formatRow($child));
  24. }
  25. }
  26. }
  27. });
  28. return $tasks;
  29. }
  30. protected function formatRow(Task $task)
  31. {
  32. $displayId = self::$index++;
  33. return [
  34. $displayId,
  35. // $task->id,
  36. $task->name,
  37. $task->begin,
  38. $task->end,
  39. $task->parent?->name,
  40. $task->project?->name,
  41. $task->asset?->name,
  42. $task->requirement?->name,
  43. $task->status,
  44. $task->task_type,
  45. $task->doc_type,
  46. $task->suitability,
  47. $task->state,
  48. $task->approval_status,
  49. $task->description,
  50. $task->createdBy?->name,
  51. $task->created_at,
  52. ];
  53. }
  54. public function headings(): array
  55. {
  56. return [
  57. 'ID',
  58. 'Name',
  59. 'Begin',
  60. 'End',
  61. 'Parent',
  62. 'Project Name',
  63. 'Asset',
  64. 'Requirement',
  65. 'Status',
  66. 'Task Type',
  67. 'Doc Type',
  68. 'Suitability',
  69. 'State',
  70. 'Approval Status',
  71. 'Description',
  72. 'Created By',
  73. 'Created Date',
  74. ];
  75. }
  76. }