3

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

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

    });
})->download('xlsx');
1
  • What is the content of your code at line 1332? Commented May 14, 2018 at 9:17

2 Answers 2

1

$export is an Eloquent collection and you are using the fromArray method so it expects an array.

You can do two things:

  1. Convert your Eloquent collection to an array:

$sheet->fromArray($export->toArray());

  1. Use the fromModel method that accepts an Eloquent collection:

$sheet->fromModel($export);

Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you have a model named "Excel" that makes the problem.

Just make an alias for "Maatwebsite/Excel" when you imported :

use Maatwebsite\Excel\Facades\Excel as MaatExcel;

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.