0

Where can I find this onlySheets Method ?

I wanna import data from Excel file that contains sheet called "Clients".

I've created a ClientsImport.php file :

<?php

namespace App\Imports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class ClientsImport implements WithMultipleSheets
{
    // MULTIPLE SHEETS
    public function sheets(): array
    {
        return [
            'clients' => new ClientsSheetImport(),
        ];
    }
    // END MULTIPLE SHEETS
}

and Then created ClientSheetImport.php file to get rows and validate data from given Excel file :

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithValidation;
// use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMappedCells;
use App\Client;

class ClientsSheetImport implements ToModel, WithValidation, WithMappedCells
{
  /**
   * @param array $row
   *
   * @return Client|null
   */
  public function model(array $row)
  {
      return new Client([
          'nrc_entrp' => $row['nrc_entrp'],
          'raisoci' => $row['raisoci'],

      ]);
  }

  public function rules(): array {
      return [
        'nrc_entrp'=> 'required|unique:clients|max:20',
        'raisoci'=> 'required|unique:clients|max:100',
      ];
  }
  public function mapping(): array
  {
      return [
        'nrc_entrp' => 'B1',
        'raisoci' => 'B2',
        
      ];
  } // mapping

}

and in ImportClientController.php I wanna import one Sheet from Excel file ("Clients" sheet") with this Given Code :

  $import_client = new ClientsImport();
  $import_client->onlySheets('clients');
  Excel::import($import_client, $file_url);

But I got this error :

Call to undefined method App\Imports\ClientsImport::onlySheets()

Where can I find this onlySheets Method ?

Thanks

2

1 Answer 1

1

I do this and it works. Adding this:

class UserImport implement ...{
use WithConditionalSheets;
 public function conditionalSheets(): array
    {
        return [
            'Sheet1' => new ReadImport(),
        ];
    }
...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.