2017-07-06 54 views
0

我有問題,我的項目,我需要我的表從搜索數據導出Laravel,我怎麼能做到這一點,先生? 有我的控制器和index.blade.php如何從Laravel當前搜索中將數據導出到excel中?

Controller.php這樣

public function downloadExcel(Request $request, $type) 
{ 
    $data = TABLENAME::get()->toArray(); 
    return Excel::create('FILENAME', function($excel) use ($data) { 
     $excel->sheet('mySheet', function($sheet) use ($data) 
     { 
      $sheet->fromArray($data); 
     }); 
    })->download($type); 
} 

public function search(Request $request) 
{ 
$query = Model_items::query(); 
      if(Input::has('name')) { 
       $query->where('name', $getName); 
      } 
      if(Input::has('phone')) { 
       $query->where('phone', $getPhone); 
      } 
      $dataList = $query->paginate(10); 
return view('index', compact('dataList')); 
} 

Index.blade.php

   <div class="col-md-4"> 
          {!! Form::open(['method'=>'GET','url'=>'findData','role'=>'search']) !!} 
          <div style="left: 120%" class="input-group custom-search-form"> 

           <input id="cTah" name="cTah" type="checkbox" style="margin-bottom: 10px"/> Name 
           <input type="text" class="iTah" name="name" disabled="" style="float: right;"><br> 

           <input id="cKab" name="cKab" type="checkbox" style="margin-bottom: 10px"/> Phone 
           <input type="text" class="iKab" name="phone" disabled="" style="float: right;"><br> 

           <br> 
           <span class="input-group-btn"> 
            <span class="input-group-btn"> 
             <button class="btn btn-default" type="submit" style="margin-left: 20px"> 
              <i class="fa fa-search"></i> Search 
             </button> 
            </span> 
           </span> 
          </div> 
          {!! Form::close() !!} 
         </div> 
        </div> 
        <br/> 
        <table class="table table-bordered table-hover table-condensed tfix"> 
         <thead align="center"> 
         <tr> 
          <td><b>NAME</b></td> 
          <td><b>PHONE</b></td> 
          <td width="85px"></td> 
         </tr> 
         </thead> 
        @foreach($dataList as $key => $view) 
         <tr align="center"> 
          <td>{{ $view->name }}</td> 
          <td>{{ $view->phone }}</td> 
         </tr> 
        @endforeach 
        </table> 
        {{ $dataList->appends(Request::except('page'))->links() }} 
      <br/> 
      <h3>Export File From Database:</h3> 
      <div style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 20px;"> 
       <a href="{{ route('pdfview',['download'=>'pdf']) }}"><button class="btn btn-danger btn-lg">Export PDF</button></a> 
       <a href="{{ url('downloadExcel/xlsx') }}"><button class="btn btn-success btn-lg">Export Excel</button></a> 
      </div> 
      <br/> 

我用瑪特把數據轉換到Excel,但這是所有出口TABLENAME中的數據,如何使此代碼可以從搜索中導出當前數據?

回答

1

簡單寫控制器代碼看起來像

if(Input::has('download')) { 
    $query = Model_items::query(); 
    if(Input::has('name')) { 
     $query->where('name', $getName); 
    } 
    if(Input::has('phone')) { 
     $query->where('phone', $getPhone); 
    } 
    $dataList = $query->get(); 

    return Excel::create('FILENAME', function($excel) use ($dataList) { 
     $excel->sheet('mySheet', function($sheet) use ($dataList) 
     { 
      $sheet->fromArray($dataList); 
     }); 
    })->download($type); 

} else { 
    $query = Model_items::query(); 
    if(Input::has('name')) { 
     $query->where('name', $getName); 
    } 
    if(Input::has('phone')) { 
     $query->where('phone', $getPhone); 
    } 
    $dataList = $query->paginate(10); 
} 

return view('index', compact('dataList'));