从 CSV 导入到 Laravel
Import from CSV to Laravel
我正在尝试导入大量记录。记录在 .CSV 文件中。
记录应该去"DocCertification"哪个型号。记录具有依赖于其他模型的字段。
我所做的是创建一个使用 TestController
的路径“/import”。我收到错误:
Trying to get property 'n_matriculated' of non-object
但我也得到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;
}
}
我不明白我应该做什么。
这是它显示的错误:
您没有在 $matriculated
变量中得到任何东西。返回前尝试检查它。
我正在尝试导入大量记录。记录在 .CSV 文件中。
记录应该去"DocCertification"哪个型号。记录具有依赖于其他模型的字段。
我所做的是创建一个使用 TestController
的路径“/import”。我收到错误:
Trying to get property 'n_matriculated' of non-object
但我也得到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;
}
}
我不明白我应该做什么。
这是它显示的错误:
您没有在 $matriculated
变量中得到任何东西。返回前尝试检查它。