在同一控制器laravel 5.6中的方法之间传递变量数据

时间:2018-07-02 05:04:08

标签: php laravel-5.6

伙计们,您如何在同一控制器的function之间传递变量?我尝试制作一个全局变量,并使用Session::来设置和获取值,但是这两种方法均无效。我从generate.blade.php获取start_dateend_date的值,并将其值传递给downloadPDF函数,以根据日期范围过滤数据。任何能启发我的人该如何完成?

GenerateReportController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

use App\Attendance;
use App\Subject;
use PDF;
use Session;  
use View;



class GenerateReportController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public $start_date;
    public $end_date;

    public function index()
    {
        $this->start_date = Input::get('startDate');
        $this->end_date = Input::get('endDate');

        $subjects = Subject::all();
        return View::make('generate', compact('subjects',$subjects));
    }

    public function downloadPDF()
    {

        $dateBetween = Attendance::whereBetween('date',array($this->start_date, $this->end_date))->get();

        //dd($dateBetween);
        $pdf = PDF::loadView('pdf',compact('dateBetween'));
        $name = "Attendance Report";
        return $pdf->stream($name.'.pdf');
    }
}

generate.blade.php

@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="page-content browse container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-bordered">
                <div class="panel-body">
                {!! 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,['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
                <br>
                <br>
                {!! Form::Label('endDate', 'End Date:') !!}<br>
                {!! Form::input('date', 'endDate', null, ['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

web.php

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

2 个答案:

答案 0 :(得分:0)

尝试如下

在途中

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

首先将此立面添加到您的控制器中

use Illuminate\Http\Request;

将函数转换为

public function downloadPDF($request)
{
    //try to print first dd($request->all())
    $dateBetween = Attendance::whereBetween('date',array($request->start_date, $request->end_date))->get();

    //dd($dateBetween);
    $pdf = PDF::loadView('pdf',compact('dateBetween'));
    $name = "Attendance Report";
    return $pdf->stream($name.'.pdf');
}

表格

{!! Form::open(['action'=>'GenerateReportController@downloadPDF']) !!}
{!! 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,['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
<br>
<br>
{!! Form::Label('endDate', 'End Date:') !!}<br>
{!! Form::input('date', 'endDate', null, ['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}

答案 1 :(得分:0)

已解决

不是将一个函数的变量传递给另一个函数。使用post方法将$request发送到第二个控制器,并使用$request->name获取值。

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

use App\Attendance;
use App\Subject;

use Session;
use PDF;
use View;

class GenerateReportController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $subjects = Subject::all();
        return View::make('generate', compact('subjects'));
    }

    public function downloadPDF(Request $request)
    {

        $dateBetween = Attendance::whereBetween('date',array($request->startDate,$request->endDate))->where('s_code',$request->id)->get();
        $pdf = PDF::loadView('pdf',compact('dateBetween'));
        $name = "Attendance Report";
        return $pdf->stream($name.'.pdf');
    }

刀片视图

{!! Form::open(['action'=>'GenerateReportController@downloadPDF','target' => '_blank']) !!}
{!! Form::Label('subject', 'Subject:') !!}
<select class="form-control" name="id">
    @foreach($subjects as $subject)
    <option value="{{$subject->id}}">{{$subject->s_name}}</option>
    @endforeach
</select>
<br>
{!! Form::Label('startDate', 'Start Date:') !!}<br>
{!! Form::date('startDate', 'startDate',['id' => 'datetimepicker','class' => 'datepicker']) !!}
<br>
<br>
{!! Form::Label('endDate', 'End Date:') !!}<br>
{!! Form::date('endDate', 'endDate',['id' => 'datetimepicker','class' => 'datepicker']) !!}
<br>
<br>
{!!Form::submit('Generate PDF',['class' => 'btn btn-primary'])!!}
{!! Form::close() !!}

web.php

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