Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

polarcubs's avatar

Laravel Administrator and Laravel Excel, CSV and XLS export

Hi,

Not sure if this is the right place, currently I'm building a excel and csv export feature into Laravel Administrator as a global action for my models so naturally I picked up upon Laravel Excel, however for reasons I'm not clear why I just couldn't get the download working. Here is my code under my model global_action config

        'download_excel' => [
            'title' => 'Download CSV',
            'messages' => [
                'active' => 'Creating the csv...',
                'success' => 'CSV created! Downloading now...',
                'error' => 'There was an error while creating the csv',
            ],
            //the Eloquent query builder is passed to the closure
            'action' => function($query)
            {
                //get all the rows for this query
                $data = $query->get();

                //do something to put it into excel
                Excel::create('classess', function ($excel) use ($data) {

                    // Set sheets
                    $excel->sheet('first sheet', function ($sheet) use ($data) {

                        // Sheet manipulation
                        $sheet->fromArray($data);
                    });

                })->export('csv');
            }
        ],

This was the same as how http://www.maatwebsite.nl/laravel-excel/docs/export described on how to start a download export. Am I missing out on anything?

Laravel Administrator docs can be found at

http://administrator.frozennode.com/docs/model-configuration#global-custom-actions

0 likes
5 replies
polarcubs's avatar
polarcubs
OP
Best Answer
Level 40

Solved it by first calling the stored method then use Response::download to reference the created file on the server.

cmarfil's avatar

@polarcubs You can post the code in action working well? Thanks!

Edit:

Here is the full solution:


        'download_excel' => [
            'title' => 'Download CSV',
            'messages' => [
                'active' => 'Creating the csv...',
                'success' => 'CSV created! Downloading now...',
                'error' => 'There was an error while creating the csv',
            ],
            //the Eloquent query builder is passed to the closure
            'action' => function($query)
            {
                //get all the rows for this query
                $data = $query->get();

                //do something to put it into excel
                Excel::create('classess', function ($excel) use ($data) {

                    // Set sheets
                    $excel->sheet('first sheet', function ($sheet) use ($data) {

                        // Sheet manipulation
                        $sheet->fromArray($data);
                    });

                })->store('csv', storage_path('excel/exports'), true);

            return Response::download($file['full']);
            }
        ],

samsoft's avatar

Great post... But I'm having problem using the Response::download($file['full']). Its generate this error.... Unable to guess the mine type as no guessers are available, I've uncomments php_file info but still receive the same error, please what can I do? I'm using PHP 5.5

sumitanand098's avatar

try using :

        $data= json_decode( json_encode($data), true);

below--

$data = $query->get();

like::

$data = $query->get();

            $data= json_decode( json_encode($data), true);

Please or to participate in this conversation.