属性 [name] 在使用 maatwebsite/excel 的集合实例上不存在:~2.1.0
Property [name] does not exist on this collection instance using maatwebsite/excel:~2.1.0
我正在尝试将 Excel 文件导入 Laravel 5.5 并将数据插入数据库。按照本教程。 https://laratutorials.wordpress.com/2017/10/03/how-to-import-excel-file-in-laravel-5-and-insert-the-data-in-the-database-laravel-tutorials/comment-page-1/#comment-262
我已经能够获取并验证所选文件。
但是出现此错误:此集合实例上不存在 属性 [name]。
public function import(Request $request){
//validate the xls file
$this->validate($request, array(
'file' => 'required'
));
if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
];
}
if(!empty($insert)){
$insertData = DB::table('students')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return back();
}
}
}
return back();
}else {
Session::flash('error', 'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
return back();
}
}
}
dd($data) 输出这个
SheetCollection {#609 ▼
#title: ""
#items: array:3 [▼
0 => RowCollection {#412 ▼
#heading: array:3 [▼
0 => "name"
1 => "email"
2 => "phone"
]
#title: "Sheet1"
#items: array:3 [▼
0 => CellCollection {#575 ▼
#title: null
#items: array:3 [▼
"name" => "emma"
"email" => "emma@yahoo.com"
"phone" => 89889898.0
]
}
1 => CellCollection {#621 ▼
#title: null
#items: array:3 [▼
"name" => "Godstime John"
"email" => "jgodstime10@yahoo.com"
"phone" => 909989898.0
]
}
2 => CellCollection {#414 ▼
#title: null
#items: array:3 [▼
"name" => "John Emma"
"email" => "jgh@email.com"
"phone" => 9090898.0
]
}
]
}
1 => RowCollection {#571 ▼
#heading: array:1 [▼
0 => ""
]
#title: "Sheet2"
#items: []
}
2 => RowCollection {#572 ▼
#heading: array:1 [▼
0 => ""
]
#title: "Sheet3"
#items: []
}
]
}
我希望能够将我的 excel 文件(姓名、电子邮件、phone)中的行上传到我的数据库中具有字段(id、姓名)的学生 table , 电子邮件, phone).
我按照上面的所有步骤 link。但我仍然收到上述错误消息。
foreach ($data[0] as $key => $value) {
// dd($value->name);
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
];
}
这是带有解释的解决方案。
这是您当前的地图方法代码。
public function map($user): array
{
return [
$user->id,
$user->email
];
}
但是我更新了用户的table数据。我已经像这样在构造函数中从控制器传递了用户数据。然后在 map 方法下的 foreach 循环中,我会将数据存储在数组中,然后 return 数组。
我希望,这解释了解决方案。
这是我的代码。
<?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersExport implements FromCollection, ShouldAutoSize, WithMapping
{
use Exportable;
protected $users;
public function __construct(array $users)
{
$this->users = $users;
}
public function collection()
{
return collect($this->users);
}
public function map($user): array
{
foreach ($this->users[0] as $key => $value) {
$user[] = [
'id' => $value->id,
'email' => $value->email
];
}
return $user;
}
}
控制器代码
if($request->export_users == 'true'){
$usersExport = User::get();
$export = new UsersExport([
$usersExport
]);
return Excel::download($export, 'users.xlsx');
}
注意:记得在controller中导入依赖
我正在尝试将 Excel 文件导入 Laravel 5.5 并将数据插入数据库。按照本教程。 https://laratutorials.wordpress.com/2017/10/03/how-to-import-excel-file-in-laravel-5-and-insert-the-data-in-the-database-laravel-tutorials/comment-page-1/#comment-262
我已经能够获取并验证所选文件。 但是出现此错误:此集合实例上不存在 属性 [name]。
public function import(Request $request){
//validate the xls file
$this->validate($request, array(
'file' => 'required'
));
if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
];
}
if(!empty($insert)){
$insertData = DB::table('students')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return back();
}
}
}
return back();
}else {
Session::flash('error', 'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
return back();
}
}
}
dd($data) 输出这个
SheetCollection {#609 ▼
#title: ""
#items: array:3 [▼
0 => RowCollection {#412 ▼
#heading: array:3 [▼
0 => "name"
1 => "email"
2 => "phone"
]
#title: "Sheet1"
#items: array:3 [▼
0 => CellCollection {#575 ▼
#title: null
#items: array:3 [▼
"name" => "emma"
"email" => "emma@yahoo.com"
"phone" => 89889898.0
]
}
1 => CellCollection {#621 ▼
#title: null
#items: array:3 [▼
"name" => "Godstime John"
"email" => "jgodstime10@yahoo.com"
"phone" => 909989898.0
]
}
2 => CellCollection {#414 ▼
#title: null
#items: array:3 [▼
"name" => "John Emma"
"email" => "jgh@email.com"
"phone" => 9090898.0
]
}
]
}
1 => RowCollection {#571 ▼
#heading: array:1 [▼
0 => ""
]
#title: "Sheet2"
#items: []
}
2 => RowCollection {#572 ▼
#heading: array:1 [▼
0 => ""
]
#title: "Sheet3"
#items: []
}
]
}
我希望能够将我的 excel 文件(姓名、电子邮件、phone)中的行上传到我的数据库中具有字段(id、姓名)的学生 table , 电子邮件, phone). 我按照上面的所有步骤 link。但我仍然收到上述错误消息。
foreach ($data[0] as $key => $value) {
// dd($value->name);
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
];
}
这是带有解释的解决方案。
这是您当前的地图方法代码。
public function map($user): array
{
return [
$user->id,
$user->email
];
}
但是我更新了用户的table数据。我已经像这样在构造函数中从控制器传递了用户数据。然后在 map 方法下的 foreach 循环中,我会将数据存储在数组中,然后 return 数组。 我希望,这解释了解决方案。 这是我的代码。
<?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersExport implements FromCollection, ShouldAutoSize, WithMapping
{
use Exportable;
protected $users;
public function __construct(array $users)
{
$this->users = $users;
}
public function collection()
{
return collect($this->users);
}
public function map($user): array
{
foreach ($this->users[0] as $key => $value) {
$user[] = [
'id' => $value->id,
'email' => $value->email
];
}
return $user;
}
}
控制器代码
if($request->export_users == 'true'){
$usersExport = User::get();
$export = new UsersExport([
$usersExport
]);
return Excel::download($export, 'users.xlsx');
}
注意:记得在controller中导入依赖