Hey guys how do u pass a variable between a function within the same controller? I have tried making a global variable and using Session:: to set and get the values but neither of the method works. I am getting the values of the start_date and end_date from my generate.blade.php and pass the value to my downloadPDF function to filter the data based on the date range. Anyone able to enlighten me how can i accomplish this?
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');
downloadPDF()and submit the form in that route. pass $request parameter indownloadPDF($request). And access it$request->start_date