我的项目存在问题,我需要从Laravel的搜索数据中导出我的表格,我怎么能这样做先生? 有我的Controller和index.blade.php
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/>
我使用Maat将数据转换为Excel,但这是导出TABLENAME中的所有数据,如何使此代码可以从搜索中导出当前数据?
答案 0 :(得分: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'));