2017-10-20 48 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

相关问题