3

I'm a newbie in Laravel, but I'm using laravel's Maatwebsite\Excel Library v3 to export excel. But I'm having some problems exporting my array data.

here is my code

    <?php

namespace App\Exports;
use App\Team;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;

class RegisteredMemberExport implements FromCollection, WithHeadings
{
    use Exportable;

    public function collection()
    {

     $data = Team::where('reg', 1)->get();       

        return collect([
            [
                'name' => $data->name,
                'email' => $data->email
            ]
        ]);
    }

    public function headings(): array
    {
        return [
            'name',
            'email'
        ];
    }

}

the collect should be

return collect
([
            [
                'name' => 'Povilas',
                'email' => '[email protected]'
            ],
            [
                'name' => 'Taylor',
                'email' => '[email protected]'
            ]
        ]);

I can't use a loop inside the collect method return. Can I please have some help?

2
  • what data do you want from coolection Commented Sep 22, 2018 at 17:37
  • What would you want to do with the loop? Commented Sep 22, 2018 at 19:09

3 Answers 3

2

You can directly filter the values you need from your Eloquent model using a list of attributes as a parameter for the get method.

$data = Team::where('reg', 1)->get(['name', 'email']);

return collect($data->toArray());
Sign up to request clarification or add additional context in comments.

Comments

0

You can use each or map or any chain method you want Laravel Collections like this:

return collect([
                 ['name' => 'Povilas','email'=>'[email protected]'],
                 ['name' => 'Taylor','email' => '[email protected]']
              ])->each(function($value){
                 return $value; // Do what you want here
         }); 

Comments

0

You can use the Maatwebsite\Excel\Concerns\FromArray concern which works the same as FromCollection.

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;

class RegisteredMemberExport implements FromArray, WithHeadings
{
    public function array(): array
    {
        return [
            [
                'name' => 'Povilas',
                'email' => '[email protected]',
            ],
            [
                'name' => 'Taylor',
                'email' => '[email protected]',
            ],
        ];
    }

    public function headings(): array
    {
        return [
            'name',
            'email',
        ];
    }
}

Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.