0
votes

I am working on import excel data in Laravel framework and using Maatwebsite/Laravel-Excel package.

Now I have followed the steps but not getting any idea after reading package documentation, so I need help to read data from the second(2nd) sheet as I have multiple sheets in my excel file.

BillingDataImport

<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class BillingDataImport implements ToCollection
{
    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
        return $collection;
    }
}

BillingController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Imports\BillingDataImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Input;

class BillingController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $import = new BillingDataImport();
        $rawData = Excel::toArray($import, $request->file('company_1'));
        var_dump($rawData);
    }
}

When I dump the code, I am getting data from the first(1st) sheet but I want to read second(2nd) sheet so can you please guide me as I am not too good Laravel and with this package.

1

1 Answers

0
votes

You need to implement WithMultipleSheets interface in your import class. It will require you to add sheets method. You will include them like these;

public function sheets(): array
{
    return [
        0 => new FirstSheetImport(),
        1 => new SecondSheetImport(),
    ];
}

Then you may select them by index. More info here