Импорт из CSV в Laravel - PullRequest
       59

Импорт из CSV в Laravel

0 голосов
/ 03 апреля 2020

Я пытаюсь импортировать много записей. Записи находятся в файле .CSV.
Записи должны go к "DocCertification", какая модель. Записи имеют поля, которые зависят от других моделей.

Я создал путь "/ import", который использует TestController. Я получаю ошибку:

Попытка получить свойство 'n_matriculated' необъекта

Но я также получаю public function getCustomerId().

Я не знаю, что я делаю неправильно.

<?php
namespace App\Http\Controllers;
use App\DocCertification;
use App\MatProfessionalDetail;
use App\AuxCustomer;
use App\AuxItem;
use Illuminate\Http\Request;
class TestController extends Controller{
    public function import(){
        $path = public_path('certificaciones.csv');
        $lines = file($path);
        $utf8_lines = array_map ('utf8_encode', $lines);
        $array = array_map(function($v){return str_getcsv($v, ";");}, $utf8_lines);
        for ($i=1; $i < sizeof($array) ; $i+1){
            $certification = new DocCertification();
            $certification-> date = $array[$i][0];
            $certification-> n_matriculated = $this->getMatriculatedId($array[$i][1]);
            $certification-> customer = $this->getCustomerId($array[$i][2]);
            $certification-> n_invoice = $array[$i][3];
            $certification-> item = $array[$i][4];
            $certification-> mod = $this->getModId($array[$i][5]);
            $certification-> obs = $array[$i][6];
            $certification-> save();
        }
    }
    public function getMatriculatedId($matriculatedID)
    {
        $matriculated = MatProfessionalDetail::where('n_matriculated', $matriculatedID)->first();
        return $matriculated->n_matriculated;
    }
    public function getCustomerId($customerID)
    {
        $customer = AuxCustomer::where('customer', $customer)->first();
        if($customer){
            return $customer->name;
        }
        $customer = new AuxCustomer();
        $customer-> name = $customerID;
        $customer-> cuil_cuit = 'CUIT';
        $customer-> c_c_number = 0;
        $customer-> dni = 0;
        $customer-> save();
        return $customer->name;
    }
    public function getModId($modID)
    {
        $mod = AuxItem::where('mod', $mod)->first();
        if ($mod){
            return $mod->name;
        }
        $mod = new AuxItem();
        $mod-> name = $modID;
        $mod-> save();
        return $mod->name;
    }
}

Я не понимаю, что я должен делать.

Это ошибка, которую он показывает: image

Ответы [ 2 ]

0 голосов
/ 07 апреля 2020

Спасибо, что ответили. Это правда, у меня было несколько ошибок, которые я мог исправить. Спасибо!

0 голосов
/ 03 апреля 2020

Вы ничего не получаете в переменной $matriculated. Попробуйте проверить это перед возвратом.

...