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
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');
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 renamingMaatwebsite\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');