根据下拉列表过滤数据选择Laravel 5.6

时间:2018-07-01 12:01:55

标签: php laravel

我正在尝试创建一个刀片视图,在其中我有一个下拉菜单,其中有来自数据库的所有Subject和一个日期选择器,在这里我可以选择开始日期和结束日期。使用我从下拉选择和日期选择器中选择的选择,我想过滤Attendance表的数据并将其显示为PDF表。到目前为止,我无法完成的工作是提取所有数据并显示为PDF。如何创建此过滤器功能?我对laravel还是比较陌生,所以请问我这样的问题。

这是我到目前为止所做的。

GenerateReportController

<?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>

已更新

generate.blade.php

该表单可以帮助根据我选择的主题过滤数据,然后当我单击“生成”时,它将根据主题名称attendance根据我的s_name表中的结果生成PDF报告。我该怎么办?

@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>  

web.php

Route::get('dashboard/attendance/generate','GenerateReportController@index'); Route::get('dashboard/attendance/report','GenerateReportController@downloadPDF');

1 个答案:

答案 0 :(得分:0)

尝试:


    $whereRawKeys = '';
    $whereRawValues = [];
    $report = new Attendance();
    $rquestQueries = [
        'Date(attendances.date)' => $request->get('date'), // datepacker input from date
        'attendances.student_id' => $request->get('student_id'), // select student name given student id
        'attendances.subject_id' => $request->get('subject_id') // select subject name given subject 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'));