Tengo un command
de laravel
que lee un archivo csv
y llamará a una función que por cada elemento, mirará si el elemento existe (lo actualizará) y si no existe lo creará.
El command
es este:
public function handle()
{
if (false === file_exists($this->argument('file'))) {
throw new FileNotFoundException('File not found');
}
$this->file = $this->argument('file');
$this->comment('--- Start reading CSV file ---');
$handle = fopen($this->file, "r");
$outPutCsvLine = [];
while ($csvLine = fgetcsv($handle, 1000, ',')) {
$data = [
'name' => $csvLine[0];
'category_id' => $this->searchCategoryByName($csvLine[1])
];
$this->createOrUpdateAliment($data);
}
}
Ahora tengo dudas con la función que creará este elemento, de momento tengo este código:
private function createOrUpdateAliment($data) {
$aliment = App\Aliment::where('name',$name)->first();
if (null === $aliment) {
$aliment->create($data);
}
else {
.... // actualizar
}
}
Tengo duda como hacer el "update
" de este objeto.
Gracias