Laravel-excel: [QUESTION] hadings with params

Created on 3 Feb 2020  路  3Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

  • PHP version: 7.4.1
  • Laravel version: 5.8
  • Package version: 3.1

Description

I would like to pass array param to headings. I don't know how to do it.

Additional Information

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

question

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

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hardikdangar picture hardikdangar  路  38Comments

adnandogar picture adnandogar  路  34Comments

MakamuEvans picture MakamuEvans  路  21Comments

abbylovesdon picture abbylovesdon  路  25Comments

adnandogar picture adnandogar  路  32Comments