我想通过CVS导入填充2个模型。第一个模型是零售店列表,第二个模型是零售店对应的位置。
这是CSV导入:
name,email,website,street_number,street_address,city,country,postcode,latitude,longitude
"Example name",info@example.com,10,"random street",amsterdam,netherlands,4000,52.666,4.3333
RetailStores.php
模型需要以下值:
name,
email,
website
LocationStores.php
模型需要以下值:
street_number,
street_address,
city,
country,
postcode,
latitude,
longitude
我将从foreach
循环中的CSV文件中提取数据并将它们附加到空数组变量,从这里将使用一些Eloquent
来填充数据库。以下是我的工作方式:
$file = Input::file('csv_file');
$data = $this->retailer->processCsv($file);
$retailers = [];
$locations = [];
foreach ($data as $value) {
$retailers[] = array(
'user_id' => Auth::user()->id,
'name' => $value['name'],
'email' => $value['email'],
'website' => $value['website'],
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
);
$locations[] = array(
'retailer_id' => $value['retailer_id'], // ISSUE HERE
'street_number' => $value['street_number'],
'street_address' => $value['street_address'],
'city' => $value['city'],
'state' => $value['state'],
'country' => $value['country'],
'country_code' => $value['country_code'],
'postcode' => $value['postcode'],
'latitude' => $value['latitude'],
'longitude' => $value['longitude'],
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
);
Retailer::insert($retailers);
Location::insert($locations);
}
如上所述,我有2个单独的模型零售商和地点。我需要在$locations
数组中引用零售商ID。现在我有一个选项,即让用户输入CSV文件中每个位置的相关零售商店ID,但人们很好......你知道。
但是考虑到CSV文件会为其拥有的每个位置提供“商店名称”,是否无法为每个唯一商店引用增量 ID 数组中的名字?例如,如果CSV表具有以下值:
Fruit,info@fruit.com,www.fruit.com,1,"apple street",etc
Fruit,info@fruit.com,www.fruit.com,120,"pear parkway",etc
Fruit,info@fruit.com,www.fruit.com,350,"orange avenue",etc
Meat,info@meat.com,www.meat.com,33,"Steak street",etc
Meat,info@meat.com,www.meat.com,33,"Chicken Road",etc
在 foreach 循环中获取这些值时,有没有办法可以为每个重复的商店名称提供增量ID,从而将相关商店引用到其位置?
任何建议,非常感谢。
答案 0 :(得分:0)
您可以存储零售商名称,以便检查特定名称是否已经在零售商阵列中,并且如果是,则获取ID。像这样:
foreach ($data as $value) {
$retailer_name = $value['name']; // for clarity
// is the retailer name in the retailers array already?
if (isset($retailers[$retailer_name])){
// get the ID
$retailer_id = $retailers[$retailer_name]['user_id'];
} else { // otherwise add a new one
$retailer_id = Auth::user()->id; //store the ID for use in Locations
// use retailer name for the key of the array, so it's easily findable later
$retailers[$retailer_name] = array(
'user_id' => $retailer_id,
'name' => $value['name'],
'email' => $value['email'],
'website' => $value['website'],
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
);
}
$locations[] = array(
'retailer_id' => $retailer_id, // add either the new or existing ID
'street_number' => $value['street_number'],
'street_address' => $value['street_address'],
'city' => $value['city'],
'state' => $value['state'],
'country' => $value['country'],
'country_code' => $value['country_code'],
'postcode' => $value['postcode'],
'latitude' => $value['latitude'],
'longitude' => $value['longitude'],
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
);
Retailer::insert($retailers);
Location::insert($locations);
}
然后,您有一个数组,其中包含字符串作为$retailers
的键,如果这是插入的问题,您必须为零售商创建一个单独的数组,或者在插入之前将其更改为数字键。