I’m trying to import a lot of records. The records are in a .CSV file.
The records should go to “DocCertification” which model. The records have fields that depend on other models.
What I did was create a path “/import” which uses the TestController
. I get the error:
Trying to get property ‘n_matriculated’ of non-object
But I also get public function getCustomerId()
.
I don’t know what I’m doing wrong.
JavaScript
x
<?php
namespace AppHttpControllers;
use AppDocCertification;
use AppMatProfessionalDetail;
use AppAuxCustomer;
use AppAuxItem;
use IlluminateHttpRequest;
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;
}
}
I don’t understand what I’m supposed to do.
Advertisement
Answer
You are not getting anything in the $matriculated
variable. Try checking it before returning.