Lokendra Parihar
Lokendra Parihar

Reputation: 131

How to manipulate data before exporting in laravel 5.7 and excel 3.1

I want to customize column names, concatinate data in one column. Can put some condition on data.

Below is sample image, In which format data would show. How to make possible this-

enter image description here

Upvotes: 0

Views: 1304

Answers (1)

Piazzi
Piazzi

Reputation: 2636

Here you can find how to manipulate columns names: https://laravel-excel.maatwebsite.nl/3.1/exports/mapping.html

And below its a example from my project in how to use it. Enjoy!

namespace App\Exports;
use App\Aluno;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class AlunosExport implements FromCollection, WithStrictNullComparison, WithHeadings, ShouldAutoSize
{
  /**
  * @return \Illuminate\Support\Collection
  */
  public function collection()
  {
      $alunos = Aluno::all();
      $dadosAluno = [];
      foreach ($alunos as $aluno) {
          $textoEtapas = '';
          foreach ($aluno->etapas as $etapa) {
              $textoEtapas .= "{$etapa->nome}";
              if ($etapa->concluido) {
                  $textoEtapas .= ' (Concluído)';
              }
              $textoEtapas .= "\n";
          }
          $dadosAluno[] = [
              'nome' => $aluno->cliente->nome,
              'telefone' => formatarTelefone($aluno->cliente->telefone),
              'instituicao' => $aluno->cliente->turma->unidade->instituicao->nome,
              'turma' => $aluno->cliente->turma->nome,
              'programa' => $aluno->programa->nome,
              'etapas' => $textoEtapas,
              'valor' => $aluno->valor,
              'orientador' => !is_null($aluno->orientador) ? $aluno->orientador->nome : '(Excluído)',
              'status' => $aluno->cliente->status
          ];
      }
      $alunoColection = collect($dadosAluno);
      return $alunoColection;
  }
  public function headings(): array
  {
      return [
          'Aluno',
          'Telefone',
          'Instituição',
          'Turma',
          'Programa',
          'Etapas',
          'Valor',
          'Orientador',
          'Status'
      ];
  }
}

Upvotes: 2

Related Questions