Marty Hemart
Marty Hemart

Reputation: 47

export excel file maatwebsite laravel

I'm blocking to export an excel file from a table on laravel here's my controller :

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Excel;

class ExportExcelController extends Controller
{
  function Export()
  {
    $customer_data = DB::table('qualys')->get();
    return view('export_excel')->with('customer_data', $customer_data);
  }
  function excel()
  {
    $customer_data = DB::table('qualys')->get();
    $customer_array[] = array('ip','qid');
    // dd($customer_data);
    foreach($customer_data as $customer)
    {
      // dd($customer);
       $customer_array[] = array(
                 'ip' => $customer->qid,
                 'qid' => $customer->ip
      );
    }


     Excel::download('customer data', function($excel) use ($customer_array) {
          $excel->setTitle('customer Data');
         $excel->sheet('Customer Data', function($sheet) use ($customer_array)
          {
            $sheet->fromArray($customer_array, null, 'A1', false, false);
          });
     })->download('xls', 'test');
  }
}

I'm blocked at line 29 level, I can't get the file back, here's the following error:

Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given, called in /var/www/html/qualys/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 223

I have tested several "code" but nothing works:( can you help me plz? cordially.

Upvotes: 1

Views: 4233

Answers (2)

Marty Hemart
Marty Hemart

Reputation: 47

It's okay, I figured out how to do it with the latest version -> First command to type :

php artisan make:export UsersExport --model=User

Then create the model the database you want to export

php artisan make:model User

Then my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Excel;
use App\Exports\QualysExport;
use App\Http\Controllers\Controller;

class ExportExcelController extends Controller
{
  function Export()
  {
    $customer_data = DB::table('user')->get();
    return view('export_excel')->with('customer_data', $customer_data);
  }

      public function excel()
      {
          return Excel::download(new UserExport, 'Users.xlsx');
      }

}

then the route to download the file:

 Route::get('/excel_export/excel', 'ExportExcelController@excel')->name('export_excel.excel');

And here comes the download! Sincerely

Upvotes: 1

Md.Sukel Ali
Md.Sukel Ali

Reputation: 3065

You can try this code,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Excel;

class ExportExcelController extends Controller
{
  function Export()
  {
    $customer_data = DB::table('qualys')->get();
    return view('export_excel')->with('customer_data', $customer_data);
  }
  function excel()
  {
    $customer_data = DB::table('qualys')->get();
    $customer_array[] = array('ip','qid');
    // dd($customer_data);
    foreach($customer_data as $customer)
    {
      // dd($customer);
       $customer_array[] = array(
                 'ip' => $customer->qid,
                 'qid' => $customer->ip
      );
    }

      // change download to create
     Excel::create('filename', function($excel) use ($customer_array) {
          $excel->setTitle('customer Data');
         $excel->sheet('Customer Data', function($sheet) use ($customer_array)
          {
            $sheet->fromArray($customer_array, null, 'A1', false, false);
          });
     })->download('xls');
     // donwload method only accept one parament and that is file extension
  }
}

Upvotes: 0

Related Questions