1

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');

3
  • Create a post/get route for downloadPDF() and submit the form in that route. pass $request parameter in downloadPDF($request). And access it $request->start_date Commented Jul 2, 2018 at 5:11
  • @EmptyBrain i have updated my route do let me know if im doing it correctly Commented Jul 2, 2018 at 5:13
  • its better to pass date as params : dashboard/attendance/report?start=start_date&end=end_date Commented Jul 2, 2018 at 5:16

2 Answers 2

2

SOLVED

Instead of passing the variable one a function to another. Use a post method to send the $request to the second controller and use $request->name to get the value.

Controller

<?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');
    }

Blade View

{!! 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');
Sign up to request clarification or add additional context in comments.

Comments

0

Try like below

In Route

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

First add this facade in your controller

use Illuminate\Http\Request;

After convert your function to

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

{!! 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() !!}

5 Comments

i have tried the solution you gave and unfortunately it did not return any data in my PDF view. I tried dd($request->start_date) and it is returning null
@DexterSiah please please try dd($request->all()) First to see your request parameters. i think you values are in date key. DID you check it??
it is returning [] am i missing anything or did u misinterpreted how i want to get my date. The 'start_date' and 'end_date' is is captured from the `index' controller( generate view) and i want to pass it to 'generatePDF' controller (pdf view)
The form should work. did you check the updates?? Other way like you saying index function already captured the value you can send it like $this->downloadPDF($start_date, $end_date)
May i know where should i put that line of code in?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.