Doubt Passing Array Laravel Routes

0

The question is as follows, in my PDF generator, when I click generate, it generates a pdf with all the information that is contained in the database tables, so I am in doubt how to only pass the array, which is found when a search is done.

Example:

Thisismyscreenwithalltheinformation,ifIclickonDownloadpdf,thegeneratedpdfwillhaveallthisinformation.

BelowisthesearchscreenwhereIcamejustaninfo:

NowifIclickonDownloadPDF,itwillgenerateapdfwithalltheinformationandnotjustwhatitwassearchedfor.

AmodificationthatIthoughttogeneratethepdfwithonlytheinformationsearched,istopassthearraythatcontainstheinformationofthetablesasaparameterforthegenerationoftheURL:

HTML,buttonthatgeneratespdf:

<p><ahref="{{ action('CatracaControler@metodopdf', $catraca) }}">Download em PDF</a>
</p>

My Route:

Route::any('/listar/pdf/{$catraca}', 'CatracaControler@metodopdf')->name('Relatorio');

The Search Request function is the one returned by the search array:

public function lista(Request $request){

    $aux = $request->texto;

    $catraca = Catraca::where('MATRICULA', 'like', '%'.$aux.'%')->orWhere('NOME', 'like', '%'.$aux.'%')->orWhere('NUM_CARTAO', 'like', '%'.$aux.'%')->orWhere('MATRICULA', 'like', '%'.$aux.'%')->get();

    return view('catraca.listagem')->with('catraca', $catraca);
}

And the function of generating the pdf that receives as a parameter the array that was generated in the above function.

public function metodopdf($catraca){

    return \PDF::loadView('catraca.layoutpdf', compact('catraca'))->setPaper('a4', 'landscape')->stream('relatorio.pdf');
}

But this gives me the following error in the route:

The error message says that the route is wrong due to missing parameters.

My question is: Is it possible to pass an array as a parameter to routes? To be captured by the Generate pdf function?

    
asked by anonymous 24.07.2018 / 15:02

3 answers

1
The helper function allows you to pass an array as a second parameter as in the route parameters, see helper route see: link

In your is passing a collection resulting from the following line of code

 $catraca = Catraca::where('MATRICULA', 'like', '%'.$aux.'%')->orWhere('NOME', 'like', '%'.$aux.'%')->orWhere('NUM_CARTAO', 'like', '%'.$aux.'%')->orWhere('MATRICULA', 'like', '%'.$aux.'%')->get();

Possible solution

In action, pass the search key you used to list

<p>
<a href="{{ action('CatracaControler@metodopdf', [$chavepesquisa]) }}">Download em PDF</a>
</p>

When generating the listing, it also passes the search key used for listing (alternatively you can create a private method to return the query for the two methods)

public function lista(Request $request){

    $aux = $request->texto;

    $catraca = Catraca::where('MATRICULA', 'like', '%'.$aux.'%')->orWhere('NOME', 'like', '%'.$aux.'%')->orWhere('NUM_CARTAO', 'like', '%'.$aux.'%')->orWhere('MATRICULA', 'like', '%'.$aux.'%')->get();

    return view('catraca.listagem')->with(['catraca'=> $catraca, 'chavepesquisa'=> $aux]);
}

To generate the pdf it receives the list key and repeats the query

public function metodopdf($chavedepesquisa){

//repete a consulta à base de dados
$catraca = Catraca::where('MATRICULA', 'like', '%'.$aux.'%')->orWhere('NOME', 'like', '%'.$aux.'%')->orWhere('NUM_CARTAO', 'like', '%'.$aux.'%')->orWhere('MATRICULA', 'like', '%'.$aux.'%')->get();


return \PDF::loadView('catraca.layoutpdf', compact('catraca'))->setPaper('a4', 'landscape')->stream('relatorio.pdf');

}

    
24.07.2018 / 17:20
1

I suggest you, instead of passing an array with the values, pass back the variable used in the search, and redo that search before generating the pdf. So you do not have to do the same thing in two different ways!

Create a function in your Catraca model:

function pesquisa($aux){
    return Catraca::where('MATRICULA', 'like', '%'.$aux.'%')->orWhere('NOME', 'like', '%'.$aux.'%')->orWhere('NUM_CARTAO', 'like', '%'.$aux.'%')->orWhere('MATRICULA', 'like', '%'.$aux.'%')->get();
}

This is done, you call the Catraca::pesquisa($aux) method wherever you want, including the pdf generation functions, and the search itself, and you can continue using only one parameter. It gets cleaner and easier to understand for others.

I hope I have helped!

    
24.07.2018 / 17:16
0

Dude, you can pass a Json on your route, use the functions json_encode () and json_decode () and good luck!

    
24.07.2018 / 16:23