我正在使用Laravel 5.8,Maatwebsite Excel 3.1和PHP 7开发出一个考勤应用程序。我希望用户能够导出到仅优于选定条件的Excel。视图刀片可以做到这一点,但是excel可以带出一切。
AttendanceController
public function show(Request $request,$user_id,$export=false)
{
$user= User::findOrFail($user_id);
$data['title']='Attendance of '.$user->name;
$render=[];
$attendance = new Attendance();
$attendance= $attendance->with('relUser');
$attendance=$attendance->where('user_id',$user_id);
if(isset($request->start_date) && isset($request->end_date))
{
$attendance=$attendance->whereBetween('date',[$request->start_date,$request->end_date]);
$render['start_date']=$request->start_date;
$render['end_date']=$request->end_date;
}elseif(isset($request->start_date))
{
$attendance=$attendance->where('date',$request->start_date);
$render['start_date']=$request->start_date;
}
if(isset($request->status))
{
$attendance=$attendance->where('status',$request->status);
$render['status']=$request->status;
}
if(!empty($export))
{
return Excel::download(new AttendanceExport($user_id), $user->name.'.xlsx');
}
$attendance= $attendance->orderBy('id','DESC');
$attendance= $attendance->paginate(10);
$attendance= $attendance->appends($render);
$data['attendances']=$attendance;
$data['user']=$user;
return view('admin.attendance.show',$data);
}
我的出勤控制器包括isset($ request),用于开始日期,结束日期和状态。
视图:show.blade.php
<div class="row">
<div class="col-sm-8">
<h4 class="page-title">{{ $title }}</h4>
</div>
</div>
<div class="row" style="margin-bottom: 10px">
{{ Form::model(request(),['method'=>'get']) }}
<div class="col-sm-3">
{{ Form::date('start_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
</div>
<div class="col-sm-3">
{{ Form::date('end_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
</div>
<div class="col-sm-4">
{{ Form::select('status',['Present'=>'Present','Absent'=>'Absent'],null,['class'=>'form-control','placeholder'=>'Please select status']) }}
</div>
<div class="col-sm-2">
{{ Form::submit('Search',['class'=>'btn btn-warning']) }}
<a href="{{ route('attendance.show',[$user->id,'export']) }}" class="btn btn-info">Export</a>
</div>
{{ Form::close() }}
</div>
<div class="row">
<div class="col-md-12">
<div>
<table class="table table-striped custom-table m-b-0 datatable">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@foreach($attendances as $attendance)
<tr>
<td>{{ $attendance->date }}</td>
<td>
{{ $attendance->in_time.'-'.$attendance->out_time }}
</td>
<td>{{ $attendance->status }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $attendances->links() }}
<a href="{{ route('attendance.index') }}" class="btn btn-warning">Back</a>
</div>
</div>
</div>
从View刀片中,我将发送到AttendanceExport,该文件将发送到Excel。
出勤出口
namespace App\Exports;
use App\Attendance;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
class AttendanceExport implements FromQuery, WithHeadings
{
protected $user_id;
public function __construct($user_id)
{
$this->user_id= $user_id;
}
use Exportable;
/**
* @return \Illuminate\Support\Collection
*/
public function query()
{
return Attendance::query()->select('id','date','in_time','out_time','status')->where('user_id',$this->user_id);
}
/*public function collection()
{
return Attendance::all();
}*/
public function headings(): array
{
return [
'Serial',
'Date',
'In Time',
'Out Time',
'Status'
];
}
但是,excel显示所有$ user_id信息。我希望将start_date,end_date和status的isset($ request)包含在Excel导出中,以便导出数据将基于此条件,就像在视图刀片中一样