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

nadniesol's avatar

Exporting an Excel File

I am using Laravel 5.4 and I want to export a record to a excel file but I got this error

Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, string given, called in C:\xampp\htdocs\www\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1332 and defined

and here is my code:

    $export = Student::all();
    Excel::create('Export Excel',function($excel) use($export){
        $excel->sheet('Sheet 1', function($sheet) use($export){
            $sheet->fromArray($export);

        });
    })->download('xlsx');
0 likes
4 replies
tykus's avatar

That error suggests that Laravel believes Excel to be an Eloquent Model; do you have an Excel model (or are you aliasing some other model to Excel) in your domain?

Just be sure to properly import the Maatwebsite (?) Excel namespace at the top of your class, e.g.

use Maatwebsite\Excel\Facades\Excel;

or (if you do have a name collision, just alias it, e.g.:

use Maatwebsite\Excel\Facades\Excel as MaatExcel;

//

MaatExcel::create('Export Excel',function($excel) use($export){
    $excel->sheet('Sheet 1', function($sheet) use($export){
        $sheet->fromArray($export);
    });
})->download('xlsx');
1 like
nadniesol's avatar

I forgot the use Maatwebsite\Excel\Facades\Excel; but when I add it still same error. and yes I have the model

Do I need to add some codes in my model?

Here is my model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Excel extends Model { // }

tykus's avatar
tykus
Best Answer
Level 104

You are probably also importing the App\Excel model; this is what I mean by a name collision.

If you alias either import you can use both inside your class; as I described earlier, I am renaming Maatwebsite\Excel\Facades\Excel as MaatExcel for the purposes of the current class:

use Maatwebsite\Excel\Facades\Excel as MaatExcel;

//

MaatExcel::create('Export Excel',function($excel) use($export){
    $excel->sheet('Sheet 1', function($sheet) use($export){
        $sheet->fromArray($export);
    });
})->download('xlsx');

An alternative approach would be to use the FQCN:

\Maatwebsite\Excel\Facades\Excel::create('Export Excel',function($excel) use($export){
    $excel->sheet('Sheet 1', function($sheet) use($export){
        $sheet->fromArray($export);
    });
})->download('xlsx');
2 likes

Please or to participate in this conversation.