如何从maatwebsite/Excel向集合添加额外数据
我正在使用包如何从maatwebsite/Excel向集合添加额外数据,excel,laravel,file-upload,Excel,Laravel,File Upload,我正在使用包maatwebsite/excelv2.1将用户导入页面 我已经上传了文件,但我想在用户面前展示他正在导入的内容和无法导入的行(因为缺少数据或用户已经存在),但为此,我想添加至少2个额外字段,1将告诉我是否可以上传行(美学目的)而订单上有一条文字告诉他为什么不能导入那一行 我得到这样的文件行 $users = Excel::load($ruta.'/'.$filename, function ($reader) { })->get(); [ { "em
maatwebsite/excelv2.1
将用户导入页面
我已经上传了文件,但我想在用户面前展示他正在导入的内容和无法导入的行(因为缺少数据或用户已经存在),但为此,我想添加至少2个额外字段,1将告诉我是否可以上传行(美学目的)而订单上有一条文字告诉他为什么不能导入那一行
我得到这样的文件行
$users = Excel::load($ruta.'/'.$filename, function ($reader) {
})->get();
[
{
"email": "test@subject.com",
"empresa": "business1",
"encargado": "person1",
"rif": "J-12345-1",
"validity": 0,
"reason": "Email is already used"
},
{
"email": "test@subject2.com",
"empresa": "business2",
"encargado": "Person 2",
"rif": "123456-2",
"validity": 0,
"reason": "the rif field is badly formatted"
}
]
这将返回此集合,对于第一个值,用户拥有电子邮件test@subject.com
已分配,第二个rif
字段格式错误
[
{
"email": "test@subject.com",
"empresa": "business1",
"encargado": "person1",
"rif": "J-12345-1",
},
{
"email": "test@subject2.com",
"empresa": "business2",
"encargado": "Person 2",
"rif": "123456-2",
}
]
我想要的是像这样添加额外的字段
$users = Excel::load($ruta.'/'.$filename, function ($reader) {
})->get();
[
{
"email": "test@subject.com",
"empresa": "business1",
"encargado": "person1",
"rif": "J-12345-1",
"validity": 0,
"reason": "Email is already used"
},
{
"email": "test@subject2.com",
"empresa": "business2",
"encargado": "Person 2",
"rif": "123456-2",
"validity": 0,
"reason": "the rif field is badly formatted"
}
]
正如我所说,这些字段不是要保存在文档中,而是要向用户显示一个图例,说明将导入什么、不导入什么以及为什么不导入
我知道我可以在回调函数中检查所需的内容,但如何将这些值插入集合?您可以对“操纵所有集合”项使用map()
方法
$users = Excel::load($ruta.'/'.$filename, function ($reader) {
})->get();
$newUsers = $users->map(function($user){
return [
"email" => $user->email,
"empresa" => $user->empresa,
"encargado" => $user->encargado,
"rif" => $user->rif,
// make your controls and add new items here
"validity" => $validate,
"reason" => $reason
]
})
但请注意,我们正在返回数组。当您尝试在迭代中访问数据时(可能在您的视图中),您不能说$user->email,它将抛出
试图获取非对象的属性
,所以您应该说$user[“email”]
…进入哪个收藏?请举例说明您想添加哪些数据,以及在哪里添加数据?请按要求进行编辑