我想在laravel中創建一個視圖,該視圖具有將excel文件導入數據庫並將其顯示在視圖內的表中的功能。將數據導入數據庫並在laravel的html表中顯示
我創建了一個控制器,它導入數據庫中的excel數據,但未能在視圖中顯示它。
請幫助解決我的問題。
很多預先感謝。
最好的問候,
殿
我使用laravel 5.5和maatwebsite擅長
public function importFileIntoDB(Request $request){
if($request->hasFile('sample_file')){
$path = $request->file('sample_file')->getRealPath();
$data = Excel::load($path, function($reader) {})->get();
if($data->count()){
foreach ($data as $key => $value) {
$arr[] = ['name' => $value->name, 'details' => $value->details];
}
if(!empty($arr)){
\DB::table('products')->insert($arr);
dd('Insert Record successfully.');
}
}
}
return back()->with($arr);
}
這是我的看法
@extends('frontpage')
@section('title', 'Dashboard')
@section('content_header')
<h1>Upload File From Excel Into Database</h1>
@stop
@section('content')
<div class="panel panel-primary">
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title" style="padding:12px 0px;font-size:25px;"><strong>Test - import export csv or excel file into database example</strong></h3>
</div>
<div class="panel-body">
@if ($message = Session::get('success'))
<div class="alert alert-success" role="alert">
{{ Session::get('success') }}
</div>
@endif
@if ($message = Session::get('error'))
<div class="alert alert-danger" role="alert">
{{ Session::get('error') }}
</div>
@endif
<h3>Import File Form:</h3>
<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 20px;" action="{{ URL::to('importExcel') }}" class="form-horizontal" method="post" enctype="multipart/form-data">
<input type="file" name="import_file" />
{{ csrf_field() }}
<br/>
<button class="btn btn-primary">Import CSV or Excel File</button>
</form>
<br/>
<h3>Import File From Database:</h3>
<div style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 20px;">
<a href="{{ url('downloadExcel/xls') }}"><button class="btn btn-success btn-lg">Download Excel xls</button></a>
<a href="{{ url('downloadExcel/xlsx') }}"><button class="btn btn-success btn-lg">Download Excel xlsx</button></a>
<a href="{{ url('downloadExcel/csv') }}"><button class="btn btn-success btn-lg">Download CSV</button></a>
</div>
<h3>Table Import List </h3>
<div style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 20px;" >
print_r($arr->count());
@if(!empty($arr))
<table class="table table-striped table-hover table-reflow">
@foreach($arr as $key=>$value)
<tr>
<th ><strong> {{ $key }}: </strong></th>
<td> {{ $value }} <td>
</tr>
@endforeach
</table>
@endif
</div>
</div>
</div>
</div>
@stop
@section('css')
<link rel="stylesheet" href="/css/admin_custom.css">
@stop
@section('js')
<script> console.log('Hi! this is frontpage'); </script>
@stop
「但是當鑑於表明它失敗了。」任何錯誤? – linktoahref
嗨linktoahref,我沒有任何錯誤,但我希望不創建的HTML表,就好像沒有數組中的行。有什麼想法?在視圖中錯過的東西? –