FromUsersQueryExportWithEagerLoad.php 1020 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Data\Stubs;
  3. use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
  4. use Illuminate\Database\Eloquent\Relations\Relation;
  5. use Illuminate\Database\Query\Builder;
  6. use Maatwebsite\Excel\Concerns\Exportable;
  7. use Maatwebsite\Excel\Concerns\FromQuery;
  8. use Maatwebsite\Excel\Concerns\WithMapping;
  9. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  10. class FromUsersQueryExportWithEagerLoad implements FromQuery, WithMapping
  11. {
  12. use Exportable;
  13. /**
  14. * @return Builder|EloquentBuilder|Relation
  15. */
  16. public function query()
  17. {
  18. return User::query()->with([
  19. 'groups' => function ($query) {
  20. $query->where('name', 'Group 1');
  21. },
  22. ])->withCount('groups');
  23. }
  24. /**
  25. * @param mixed $row
  26. * @return array
  27. */
  28. public function map($row): array
  29. {
  30. return [
  31. $row->name,
  32. $row->groups_count,
  33. $row->groups->implode('name', ', '),
  34. ];
  35. }
  36. }