我在这个项目中使用laravel-5.4和“maatwebsite/excel”
我打算为我的网站创建一个功能,我可以在其中上传一个 excel 文件,然后在我的视图中显示这个文件的数据
我用来上传数据并将数据发送到后端的表单
public function bundleaddstudent(Request $request)
{
if($request->hasFile('excelstudent')){
$File = $request->file('excelstudent');
$path = $File->getRealPath();
$data = Excel::load($path, function($reader) {
})->toObject();
return view('User.content.tobe_added_students')
->with('selection', $this->selection())
->with('Students', $data);
}
else
{
return view('User.content.Add_student')
->with('selection', $this->selection())
->with('Students', 'No Data')
->with('request', $request);
}
}
现在,已上传并在我的控制器中处理的文件将其转换为数据对象,并且还与新视图一起传递。
我现在需要在我的视图中处理传递的数据,这就是我所做的
@isset($Students)
@foreach ($Students as $key => $value)
<tr>
<td> {{$value}}</td>
</tr>
@endforeach
@endisset
但可悲的是,这个输出是这样的
如果我尝试{{ $value->dump() }}
会发生错误,说方法转储不存在。
如果我尝试 {{ $key }}
输出是一个从 0 - 4 开始的整数输出它就像
----------------------------
| studentID | Firstname |
----------------------------
| 1 | |
| 2 | |
| 3 | |
| 4 | |
你明白了
我也尝试过{{ $value->studentId }}
,但它什么也没显示
基于我应该尝试的@LrdArc 的评论
@isset( $Students )
{{ dd( $Students ) }}
@endisset
输出:
LaravelExcelReader {#353 ▼
+excel: PHPExcel {#361 ▶}
+reader: PHPExcel_Reader_Excel2007 {#354 ▶}
+file: "/tmp/phpu3zVBH"
+columns: []
+title: "phpu3zVBH"
+ext: ""
+encoding: false
+format: "Excel2007"
+parsed: RowCollection {#430 ▼
#title: "Sheet1"
#items: array:5 [▼
0 => CellCollection {#495 ▼
#title: null
#items: array:9 [▼
"studentid" => "15-2000332"
"firstname" => "Dummy1"
"lastname" => "Dummy1"
"middlename" => "Dummy1"
"course" => "Dummy1"
"ay" => "Dummy1"
"gender" => "Dummy1"
"address" => "Dummy1"
"contact" => "Dummy1"
]
}
1 => CellCollection {#496 ▼
#title: null
#items: array:9 [▼
"studentid" => "15-2000333"
"firstname" => "Dummy2"
"lastname" => "Dummy2"
"middlename" => "Dummy2"
"course" => "Dummy2"
"ay" => "Dummy2"
"gender" => "Dummy2"
"address" => "Dummy2"
"contact" => "Dummy2"
]
}
2 => CellCollection {#515 ▶}
3 => CellCollection {#514 ▶}
4 => CellCollection {#513 ▶}
]
}