Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Decoding the Error: Fixing "Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given"
As a senior developer working with data manipulation in Laravel, dealing with external libraries like Maatwebsite\Excel is a daily occurrence. Sometimes, the library throws cryptic errors that seem completely unrelated to the code you wrote. Today, we are diving deep into a very specific error: Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given.
This post will dissect why this error occurs in your context and provide the definitive solution, ensuring your Excel downloads execute flawlessly.
Understanding the Error Context
The error message points directly to an issue with the arguments being passed to the Excel::download() method. In essence, the method expects a specific type—a string (likely a file path or stream identifier)—as its second argument, but instead, it is receiving an object when it expects a simple string.
Let's look at your provided code snippet:
Excel::download('Student_Data', function($excel) use ($student_array){
$excel->setTitle('Student Datas');
$excel->sheet('Student_Datass', function($sheet) use ($student_array){
$sheet->fromArray($student_array, null, 'A1', false, false);
});
})-->download('xlsx');
The problem isn't necessarily the first argument ('Student_Data'), but how you are structuring the callback functions within download(). While the intention is clear—to define the structure of the sheet—the way Maatwebsite\Excel handles the download stream expects a specific input format for defining the file content, and passing an anonymous function (which results in an object context when executed) directly can trigger this type mismatch.
The Root Cause: Misunderstanding the download() Signature
The core issue lies in how you are attempting to map your dynamic data structure (the $student_array) into the download process using a closure. Although Laravel facades and libraries often use flexible methods, adhering strictly to the library's contract is crucial for stability, especially when dealing with file operations.
When Excel::download() expects an argument that defines what to write, it requires either a string pointing to a file or an object that implements specific interfaces. When you pass a complex callback structure, the underlying mechanism fails because it cannot resolve the expected type for the second parameter in its execution path.
The Solution: Using Dedicated Classes for Data Export
Instead of manually orchestrating every step inside the download() closure, the most robust and idiomatic way to use Maatwebsite\Excel is to leverage its dedicated data handling classes, such as FromCollection or FromQuery. These classes handle the complex conversion from your PHP arrays/collections directly into the Excel file format, abstracting away the low-level stream management that causes type errors.
Here is the corrected approach using the FromCollection class:
Corrected Implementation Example
First, ensure you have imported the necessary class if you are using it outside of a specific facade context (though the facade often handles this implicitly).
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\FromArray; // Import the trait/concern
use Illuminate\Support\Facades\DB;
public function excel_report()
{
$student_data = DB::table('student_details')->get()->toArray();
// 1. Prepare the data structure
$header = ['Student Name', 'Address', 'Roll No', 'Class'];
$dataRows = [];
foreach ($student_data as $student) {
$dataRows[] = [
$student->st_name,
$student->address,
$student->roll_no,
$student->st_class
];
}
// 2. Use FromArray to define how the data maps directly to the sheet
Excel::download(
'Student_Data', // The file name (String)
new FromArray($dataRows, $header), // Pass an object implementing the contract
'xlsx' // The file extension to download
);
}
Why This Works Better
By switching from a complex callback approach inside download() to using dedicated classes like FromArray, you are adhering to the library’s intended design. You are no longer trying to manually define sheet structures via nested functions, which is where the type conflict arose. The FromArray class handles all the necessary stream writing and formatting internally, ensuring that the arguments passed to the core download mechanism remain strictly strings or expected object types.
Conclusion: Best Practices for Excel Exports
When working with powerful packages like Maatwebsite\Excel within Laravel, always prioritize using the provided data transfer objects (DTOs) or concerns (FromArray, FromQuery). This practice leads to code that is not only more readable but also significantly less prone to those frustrating runtime type errors. If you are building complex reports, remember that clean separation of concerns—handling data logic separately from file I/O—is the key to scalable development in Laravel. For deeper insights into leveraging Eloquent and database interactions effectively, exploring resources from the official team at laravelcompany.com is highly recommended.