Call to undefined method Maatwebsite\Excel\Excel::create() laravel-5.8
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Resolving "Call to undefined method Maatwebsite\Excel\Excel::create()" in Laravel Excel
As a senior developer working with the Laravel ecosystem, dealing with package methods that suddenly become "undefined" can be frustrating. You are trying to achieve a common goal—exporting database results into an Excel file—but the system throws an error. This often signals an issue related to package versioning, class loading, or misunderstanding the specific API of the library you are using.
This post will diagnose why you are seeing the error Call to undefined method Maatwebsite\Excel\Excel::create() when attempting to use the Laravel Excel package (maatwebsite/excel), and provide robust, modern solutions.
Understanding the Error: Why is the Method Undefined?
The error Call to undefined method Maatwebsite\Excel\Excel::create() means that the PHP runtime cannot find a method named create() directly on the Excel class instance you are calling. While your provided code snippet looks syntactically correct for using the package, this usually points to one of three core issues:
- Outdated Package Version: The most common cause is using an older version of the
maatwebsite/excelpackage that has a different API structure than what you are attempting to call. Newer versions often refactor methods or require specific class usage. - Incorrect Facade Usage: You might be calling the static method incorrectly, or the necessary
usestatements for namespaces are missing in your controller file, leading PHP to fail resolving the method contextually. - Installation/Autoloading Failure: Although less likely if other Laravel features work, ensure that Composer has correctly installed and autoloaded all dependencies.
When working with powerful packages like those found on laravelcompany.com, always verify your dependency versions against the package documentation to ensure compatibility, especially when dealing with framework versions like Laravel 5.8.
The Solution: Modernizing Your Excel Export
The method you are trying to use (create()) is part of the older or more manual way of interacting with the Excel writer. For modern Laravel applications, the maatwebsite/excel package provides much cleaner, collection-based methods that eliminate the need for complex manual array manipulation and often resolve these API errors automatically.
Instead of manually fetching data and building arrays, we should leverage Eloquent collections or query builders directly. This approach is safer, more readable, and aligns better with Laravel's philosophy of expressive code.
Recommended Approach: Exporting Data via Collections
The most robust way to handle this export is by passing a Collection (or an Eloquent result) directly to the toExcel method, which handles the entire creation and download process internally.
Here is how you can refactor your controller logic to achieve the same goal cleanly:
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\ImportPdf; // Assuming you have an Eloquent model
class PdfController extends Controller
{
public function excel()
{
// 1. Fetch the data directly as a Collection or Eloquent results
$pdfData = DB::table('importpdfs')->get();
// 2. Prepare the data for export (optional, but cleaner)
// If you need specific columns, map them here:
$dataToExport = $pdfData->map(function ($pdf) {
return [
'Battery' => $pdf->Battery,
'No_of_questions_attempted' => $pdf->No_of_questions_attempted,
'SAS' => $pdf->SAS,
'NPR' => $pdf->NPR,
'ST' => $pdf->ST,
'GR' => $pdf->GR,
];
});
// 3. Use the streamlined Excel facade method to export the data
Excel::store(new \Illuminate\Support\Collection($dataToExport), 'Pdf Data Export.xlsx');
// 4. Download the file
return Excel::download('Pdf Data Export.xlsx');
}
}
Why This Approach is Superior
- Reduced Error Surface: By using
Excel::store()orExcel::download()with a Collection, you delegate the complex object creation to the library itself. This significantly reduces the chance of calling undefined methods, as you are using the intended public interface provided by the package. - Readability: The code clearly states what you want to do (store this data) rather than detailing how to build every sheet and cell manually.
- Maintainability: If future versions of
maatwebsite/excelintroduce changes, using the high-level methods ensures your export logic remains resilient.
Conclusion
The error you encountered is a classic symptom of an API mismatch or outdated package interaction. While manual array creation can work, it introduces fragility. As a senior developer, our goal should always be to utilize the provided tools in the most efficient and stable way. By moving away from manually calling methods like Excel::create() and embracing the collection-based methods offered by the Laravel Excel package, you ensure your data exports are not only functional but also maintainable and scalable. Keep building robust applications using the best practices available in the Laravel ecosystem!