php - 基于下拉选择Laravel 5.6过滤数据
问题描述
我正在尝试制作一个刀片视图,其中我有一个下拉选择,其中包含Subject
来自数据库的所有内容和一个日期选择器,我可以在其中选择开始日期和结束日期。使用我从下拉选择器和日期选择器中选择的选项,我想过滤我的Attendance
表格数据并将其显示为 PDF 表格。到目前为止,我能够完成的是提取所有数据并显示为 PDF。如何创建此过滤器功能?我对 laravel 还是比较陌生,所以请原谅我问这样的问题。
这就是我到目前为止所做的。
生成报告控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Attendance;
use PDF;
class GenerateReportController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$subjects = Subject::all(['id','s_name']);
//$attendance = Attendance::all();
return View::make('generate', compact('subjects',$subjects));
}
public function downloadPDF()
{
$report = Attendance::all();
$pdf = PDF::loadView('pdf',compact('report'));
$name = "Attendance Report";
return $pdf->stream($name.'.pdf');
}
}
pdf.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Attendance Report</title>
</head>
<body>
<h1>Attendance Report</h1>
<p>Generated by {{Auth::user()->name}}</p>
<p>Date Generated : {{ date('Y-m-d') }}</p>
<p>Time Generated : {{ date('h:i:s a')}}</p>
<table cellpadding="10">
<thead>
<tr>
<th>Student Name</th>
<th>Subject</th>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@foreach($report as $repo)
<tr>
<td>{{$repo->student->stud_name}}</td>
<td>{{$repo->subject->s_name}}</td>
<td>{{$repo->date}}</td>
<td>{{$repo->time}}</td>
<td>{{$repo->att_status}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
更新
生成.blade.php
该表单有助于根据我选择的主题过滤数据,然后当我单击“生成报告”时,它将attendance
根据主题名称从我的表中生成包含结果的 PDF。s_name
我怎样才能做到这一点?
@extends('master')
@section('page_header')
<div class="container-fluid">
<h1 class="page-title">Attendance Records</h1>
<a href="/dashboard/attendance/report/" target="_blank" class="btn btn-primary">
<i class="voyager-list" style="font-size:15px;"></i>
<span>Generate Report</span>
</a>
</div>
@endsection
@section('content')
<div class="form-group">
{!!Form::Label('subject', 'Subject:')!!}
<select class="form-control" name="s_name">
@foreach($subjects as $subject)
<option value="{{$subject->s_name}}">{{$subject->s_name}}</option>
@endforeach
</select>
<br>
{!! Form::Label('startDate', 'Start Date:') !!}<br>
{!! Form::input('date', 'startDate', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}
<br>
<br>
{!! Form::Label('endDate', 'End Date:') !!}<br>
{!! Form::input('date', 'endDate', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}
</div>
@endsection
<script type="text/javascript">
$('.date').datepicker({
format: 'mm-dd-yyyy',
orientation: 'bottom'
});
</script>
网页.php
Route::get('dashboard/attendance/generate','GenerateReportController@index');
Route::get('dashboard/attendance/report','GenerateReportController@downloadPDF');
解决方案
尝试这个:
$whereRawKeys = ''; $whereRawValues = []; $report = 新出勤率(); $rquestQueries = [ 'Date(attendances.date)' => $request->get('date'), // datepacker 从日期输入 'attendances.student_id' => $request->get('student_id'), // 根据学生 ID 选择学生姓名 'attendances.subject_id' => $request->get('subject_id') // 给定主题 ID 选择主题名称 ]; foreach ($rquestQueries as $key => $value) { if (!is_null($value) && $value != '') { $whereRawKeys .= ($key . ' = ? AND '); $whereRawValues = array_merge($whereRawValues, [$value]); } } $whereRawKeys = substr($whereRawKeys, 0, -4); $report = $report->whereRaw($whereRawKeys, $whereRawValues); $report->leftJoin('students', 'students.id', '=', 'attendances.student_id') ->leftJoin('subjects', 'subjects.id', '=', 'attendances.subject_id'); $report = $report->selectRaw('attendances.*,students.stud_name,subjects.s_name')->get(); $pdf = PDF::loadView('pdf', compact('report'));
推荐阅读
- javascript - 无法在 NetCore 2 上添加 ReactJS.NET
- netsuite - NetSuite - 保存的搜索 - 动态日期范围公式
- azure-resource-manager - 部署 ARM 模板时出现内部服务器错误
- cytoscape.js - 带有 Dagre 布局的 Cytoscape.js 可以绘制垂直树吗?
- java - Eclipselink 尝试在表中插入空值
- scala - 将 List[Any] 中的每个对象转换为特定类型?
- mysql - 如何在mysql中设置本地列表/元组变量
- regex - 如何将正则表达式应用于 MongoDB 字段
- c# - 将 MongoDB.Driver 从 2.7.0 升级到 2.7.1 后连接超时
- javascript - 如何处理 Ajax/POST/PHP