2017-10-20 45 views
0

我想在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 
+0

「但是當鑑於表明它失敗了。」任何錯誤? – linktoahref

+0

嗨linktoahref,我沒有任何錯誤,但我希望不創建的HTML表,就好像沒有數組中的行。有什麼想法?在視圖中錯過的東西? –

回答

0

如@gbalduzzi輸送您需要將數據作爲

return back()->with(['arr' => $arr]); 
在你看來

和訪問返回視圖

@if(session()->has('arr')) 
<table class="table table-striped table-hover table-reflow"> 
@php $arr = session('arr'); @endphp 
@foreach($arr as $key => $value) 
    <tr> 
     <th ><strong> {{ $value['name'] }}: </strong></th> 
     <td>{{ $value['details'] }} <td> 
    </tr> 
@endforeach 
</table> 
@endif 

Docs

+0

hi linktoahref, –

+0

hi linktoahref,我按照你的代碼,但我得到錯誤。未定義的arr。有什麼想法? –

+0

你可以在你的視圖中做'dd(session('arr'))'並顯示你得到了什麼嗎?並請嘗試更新的代碼 – linktoahref

0

變化

return back()->with($arr); 

return back()->with(['arr' => $arr]); 
return back()->with('arr', $arr); // Should be equivalent 
return back()->compact($arr); // Should be equivalent 

with方法不接受僅單個值:需要指定的數組,一個鍵和一個值或使用compact方法

+0

嗨gbaldduzzi,謝謝你的回答,我已經改變了回來與緊湊。但我sitll無法看到我的網頁中的HTML表格。你見過我的觀點嗎?我錯過了什麼,或者我犯了什麼錯誤?謝謝你的幫助。 –

+0

嗨,如果你沒有得到任何錯誤,但表格是空的,這意味着你的'$ arr'是空的。在你的控制器內部調用'return back() - > ...'之前,你能添加一個'dd($ arr)'嗎? – gbalduzzi

相關問題