phplaravelmaatwebsite-excel

How to Send data from Controllet to Import Multiple Sheet on Maatwebsite Laravel


Hallo i get a trouble when i want to import excel with another data in mysql.

This is the controller

public function upload(Request $request){

  $file = $request->filebabp;
  if($file) {
    $nama_file = Carbon::now()->setTimezone('Asia/Jakarta')->format('Ymd-His').'-'.$file->getClientOriginalName();

    $datasubmit['file'] = $file;
    $datasubmit['upload_date'] = Carbon::now()->setTimezone('Europe/Belarusia')->format('Y-m-d H:i:s');
    $datasubmit['nama_file'] = $nama_file;
    $datasubmit['version'] = 'NEW';

    $file->move(public_path('files/'), $nama_file);
    $upload = Excel::import(new TargetUpload($datasubmit), base_path('public/files/'.$nama_file));
}
  return $this->sendResponse($file, 'Submit successfully');
}

And this is the Import Multiple Sheet

use Importable;
// /**
// * @param array $row
// *
// * @return \Illuminate\Database\Eloquent\Model|null
// */
protected $data;

function __construct($data) {
        $this->data = $data;
}

public function sheets($data): array
{
    return [
        0 => new TargetHeaderImport($data),
    ];
}

I want sending the datasubmit to Import multiple sheet, and i just get a error in this.

Undefined property: App\Imports\\TargetSheetImport::$data

If i import on direct 'TargetHeaderImport' its safe, but in excel i have a 2 sheets and i just only want one sheet to import.

or do you have a another script to send the 'submitdata' ?


Solution

  • If i import on direct 'TargetHeaderImport' its safe, but in excel i have a 2 sheets and i just only want one sheet to import.

    The sheets method has no parameters in its protocol and you have to tell which sheet you want to import by the return array (https://docs.laravel-excel.com/3.1/imports/multiple-sheets.html).

    Obtain $data from $this->data instead of a parameter that does not exists and if the construction of your sheet importer needs it.

    public function sheets(): array
    {
        return [
            'THIS THE SHEET NAME I WANT IMPORT - ONLY ONE!'
             => new TargetHeaderImport($this->data),
                                       ###########
                                        #########
                                         #######
                                          #####
                                           ###
                                            #
    
                           //     and this is the data     //
        ];
    }