Doubt Passing Laravel Array Routes

Asked

Viewed 789 times

0

The doubt is the following, in my PDF generator, when I click on generate, it generates a pdf with all the information that is contained in the tables of the database, so I’m in doubt dom how to pass only the array, which is found when a search is made.

Example:

This is my screen with all the information, if I click on Download pdf, the generated pdf will have all this information.

Below is the search screen where I came only one information:

inserir a descrição da imagem aqui

Now if I click on Download PDF, it will generate a pdf with all the information and not only with what was searched.

A modification that I thought for him to generate the pdf only with the researched information, is to pass the array that contains the information of the tables as parameter for the generation of the URL:

HTML, button that generates pdf:

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

My Route:

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

The Query Request function, is the one that returns 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 parameter the array that was generated in the function above.

public function metodopdf($catraca){

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

But this generates me the following error on the route:

inserir a descrição da imagem aqui

Error message says the route is wrong due to lack of parameters.

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

3 answers

2


The function (helper) allows you to pass an array as a second parameter such as in the route parameters, see helper route see: https://laravel.com/docs/5.6/helpers#method-route

In yours 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

No action passes the search key you used to list

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

When generating the listing you also pass to the view the search key used for listing (alternatively you can create a private method to return the query to both 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 receives the listing 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');

}

  • appeared this way: "Too few Arguments to Function App Http Controllers Ratcheacontroler::metodopdf(), 0 passed and Exactly 1 expected"

  • The action expects an action array('Catracacontroler@metodopdf', [$chavepesquisa]), already corrected in the example

1

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

Create a role in your Ratchet 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 done, you call the method Catraca::pesquisa($aux) where you want, including the pdf generation functions, and the search itself, and you can continue using only one parameter. Gets cleaner and easier to understand for others.

I hope I’ve helped!

  • I didn’t quite understand how you wanted to do :x

  • What exactly don’t you understand? But, the fact is that you want to pass an array of your search results, right? So how about, instead of passing the array of the search result, do the search again? More practical because, look: You sent the search term to the server, received the result, and now you want to send the result back to the server itself! What I said was basically, do the research again!

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.