I would like to pass array param to headings. I don't know how to do it.
I have made this code:
public function headings($arrayconsumo): array
{
$year = $arrayconsumo['anno'];
$month = $arrayconsumo['mes_col'];
return [
'Ref.Cliente',
'Nombre - Cliente',
'Nombre',
"$month "."$year",
];
}
but it doesn't work...
Thank you
You have to pass it through the constructor, I'd highly recommend reading the architecture documentation about export objects: https://docs.laravel-excel.com/3.1/architecture/objects.html
So instead of working on Constructor I just managed to do it differently.
Here is the code
<?php
namespace App\Imports;
use App\Country;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Ramsey\Uuid\Uuid;
class CountryImport implements ToModel, WithValidation, WithHeadingRow, WithBatchInserts, WithChunkReading,SkipsOnFailure
{
/**
* @param array $row
*/
public function model(array $row)
{
$country = new Country();
$country->create([
'id' => Uuid::uuid4(),
'name' => $row['name'],
]);
}
}
Used a Ramsey Uuid and change the function model by creating a new Model instance and use the creat() function and return nothing.
For reference, if you call ->create() in model, there's no need for the WithBatchInserts concern anymore.
Most helpful comment
You have to pass it through the constructor, I'd highly recommend reading the architecture documentation about export objects: https://docs.laravel-excel.com/3.1/architecture/objects.html