Anna Jeanine
Anna Jeanine

Reputation: 4125

Passing variable from button to controller Laravel

I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.

result page

<form action="generatePDFpage" method="get">
      <button type="submit" class="btn btn-default">Generate PDF!</button>
</form>

routes.php

Route::get('/dashboard/result/generatePDFpage', 'resultController@GeneratePDFc');

GeneratePDFc controller

    public function GeneratePDFc(){

        $id_array_implode = "HALLO";    
        $pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');    
        return $pdf->stream('invoice.pdf');
    }

So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!

Could someone please help me out? :-)

Upvotes: 2

Views: 15662

Answers (2)

MounirOnGithub
MounirOnGithub

Reputation: 699

I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?

I've found it here : https://laravel.com/docs/4.2/html#opening-a-form

And access the variable in your controller using the $request var

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

When you're using get method, you can do just this:

<a href="{{ route('route.name', $parameter) }}">
    <button type="submit" class="btn btn-default">Generate PDF!</button>
</a>

For other methods you can use something like this (this one is for DELETE method):

<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
    <input name="_method" type="hidden" value="DELETE">
    {{ csrf_field() }}

    <button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>

To get variable, use something like this:

public function generatePDF(Request $request)
{
    $someVariable = $request->someVariable;

Upvotes: 8

Related Questions