TaskExport.php 2.0 KB

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