As I indicate in the title I need to be able to import a cvs but there are certain parameters that are obtained from a form in which I cannot recover when said file is imported, these fields come from the aforementioned and I pass them as variables in the construct of the class from the importer I leave the code and tell me what I am doing wrong since these parameters always pass as null
controller
(new MayorImport($request->input('cliente_id'), $request->input('soft_contable')))->queue($file, 'public');
Note: The file is uploaded and runs in the background so I think the request doesn't work when starting the commandphp artisan queue:work
<?php
namespace App\Imports;
use App\CuentaContabilidad;
use App\Mayor;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Session;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class MayorImport implements ToModel, WithHeadingRow, WithCustomCsvSettings, WithChunkReading, ShouldQueue
{
use Importable;
protected $cliente_id;
protected $soft_contable;
public function __construct($cliente_id, $soft_contable)
{
$this->cliente_id = $cliente_id;
$this->soft_contable = $soft_contable;
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
if (!CuentaContabilidad::where('codigo', $row['cuenta'])->where('cliente_id', $this->cliente_id)->exists()) {
$centro_costo = CuentaContabilidad::create([
'rut_cliente' => $row['rut_empresa'],
'cliente_id' => $this->cliente_id,
'codigo' => $row['cuenta'],
'soft_contable' => Session::get('soft_contable'),
'nombre' => $row['nombre'],
]);
return new Mayor([
'cliente_id' => Session::get('cliente_id'),
'periodo' => $row['periodo'],
'cuenta_id' => $centro_costo->id,
'comp_nro' => $row['comprobante'],
'comp_tipo' => $row['tipo'],
'valor_cargo' => $row['debe'],
'valor_abono' => $row['haber'],
'cc_nom' => $row['centro_costo'],
'rut' => $row['rut'],
]);
}
}
public function chunkSize(): int
{
return 1000;
}
public function getCsvSettings(): array
{
return [
'delimiter' => ";",
];
}
}
When doing dd($this->cliente_id)
this it returns a null in the console
[2021-09-22 01:04:09][2] Processing: Maatwebsite\Excel\Jobs\ReadChunk
null
You should put the public attributes in the file, like so