Laravel: Import & Read Excel file (Use chunk to avoid memory fatal error)

Hi, i’m using Maatwebsite/Larabel-Excel module to upload & read the Excel file in Laravel. This tools also support large Excel file as it is using ‘chunk’ to avoid PHP memory fatal error.

For the installation, please refer to the page.

Here is my Upload Form (resources/view/import.blade.php):


            {!! Form::open( ['route' => 'import.excel', 'id' => 'form_import_excel', 'files' => true] ) !!}

Select file to upload:

            {!! Form::file('importExcel'); !!}

               {!! Form::submit( 'Upload', ['class' => 'btn btn-primary', 'id' => 'btn-submit-edit'] ) !!}

            {!! Form::close() !!}
         </div><!-- /.box-body -->
      </div><!-- /.col -->
</div><!-- /.row -->


Route (app/Http/routes.php):

//Import Excel
 Route::get( 'import/excel', ['as' => 'import.excel', 'uses' => 'ImportController@excel']);
 Route::post( 'import/excel', ['as' => '', 'uses' => 'ImportController@postExcel']);


Controller (app/Http/Controllers/ImportController.php):

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\Http\Controllers;
use App\User;
use Validator;
use App\Repositories\AuditRepository as Audit;

use Auth;
use Flash;
use Excel;

class ImportController extends Controller

 * @return \Illuminate\Http\RedirectResponse
 public function index()

 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 public function excel()
     $page_title = "Import Excel";
     $page_description = "Import & Read the Excel file";

     return view('import', compact('page_title', 'page_description'));
 public function postExcel(Request $request)
     $destination_path = storage_path('uploads');
     $_arr_excel_ext = array('xls', 'xlsx');
     if (!$request->file('importExcel')->isValid()) {
        Flash::error("Sorry, there is no file attached on the form!");
        return redirect()->route('import.excel');
     $file = $request->file('importExcel');
     if (!in_array($file->getClientOriginalExtension(), $_arr_excel_ext)) {
        Flash::error("Sorry, please use .xls or .xlsx extension!");
        return redirect()->route('import.excel');
     if (!$file->move($destination_path, $file->getClientOriginalName())) {
        Flash::error("Sorry, error on loading excel!");
        return redirect()->route('import.excel');

     //use 'chunk' to read large excel data file
     Excel::filter('chunk')->load($file)->chunk(250, function($results)
          foreach($results as $row)
                //save in database or do whatever you like here
                echo "<br>" . $row['column_name_in_lowercase'];

Good Luck.. ok Bye 😉


One thought on “Laravel: Import & Read Excel file (Use chunk to avoid memory fatal error)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s