Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Patwan's avatar

Issue generating excel in maatwebsite 3.1 in Laravel 6.4

Am working on a Laravel application whereby am trying to generate excel from data in 2 tables on my database. Have used SQL joins to join data from the database tables which works fine. Am trying to create an excel from te data (using Maatwebsite Laravel Excel package 3.1), then store the excel in a folder in my application, then sent the excel as an attachment of an email body.

The problem is when I run the command to generate the excel, I keep getting this error.

Symfony\Component\Debug\Exception\FatalThrowableError : Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given, called in C:\xampp\htdocs\jub\ven dor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 261

I have followed the documentation and other solutions on the web but seems am missing out on something in my logic.

  1. Export Class from the documentation
php artisan make:export MotorSalesExport
  1. MotorSalesExport.php file
  <?php

        namespace App\Exports;

        use Modules\Motor\Entities\User;
        use Modules\Motor\Entities\Quotation;
        use Maatwebsite\Excel\Concerns\FromCollection;
        use DB;

        class MotorSalesExport implements FromCollection
        {
            /**
            * @return \Illuminate\Support\Collection
            */
            public function collection()
            {
                 //Combine data in Quotations and users table
                $allSales = DB::select('SELECT q.quotation_no, q.policy_number,  q.premium_amount,
                q.balance, q.business_type, q.risk_note, q.debit_note, q.output_status,
                u.first_name , u.middle_name, u.surname, u.email, u.phone, u.email, u.kra_pin, u.id_number
                FROM digital_lab_motor_quotations q INNER JOIN digital_lab_motor_users u ON 
                u.id=q.user_id WHERE q.payment_status = 2 AND payment_status= 2');

                return $allSales;
            }
        }
  1. Artisan Command to generate the excel
  <?php

    namespace App\Console\Commands\General;

    use DB;
    use Carbon\Carbon;
    use Illuminate\Console\Command;
    use Mail;
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use App\Exports\MotorSalesExport;
    use Maatwebsite\Excel\Facades\Excel;


    class GenerateMotorSales extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'generate-motor-sales';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Queries all motor sales made online and generate an excel';

        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }

        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            $this->export();
        }


        public function export() 
        {

            $storage = public_path('documents\general\motor');
            $file_name = 'Sales_' . Carbon::now();
            $allData = new MotorSalesExport;

            Excel::download($file_name, function ($excel) use ($allData) {

                $excel->sheet('Motor Customers', function ($sheet) use ($allData) {
                    $sheet->fromArray($allData, '', 0, false, false);
                });
            })->store('xls', $storage);
        }
    }
0 likes
4 replies
bobbybouwmann's avatar

I think the problem here is $file_name.

$file_name = 'Sales_' . Carbon::now();

Above is a string combintes with an object. Try this instead:

$file_name = 'Sales_' . Carbon::now()->format('Y_m_d');

Now they are both strings and this is now a valid string,

Patwan's avatar

@bobbybouwmann $file_name variable is passed as 1st argument but the error says argument 2 should be a string,,

This is what I have changed to

  public function export() 
    {

        $storage = public_path('documents\general\motor');
        $file_name = 'Sales_' . Carbon::now()->format('Y_m_d');
        $allData = new MotorSalesExport;

        Excel::download($file_name, function ($excel) use ($allData) {

            $excel->sheet('Motor Customers', function ($sheet) use ($allData) {
                $sheet->fromArray($allData, '', 0, false, false);
            });
        })->store('xls', $storage);
      
    }

Please or to participate in this conversation.