Pass view data array to Laravel controller

Asked

Viewed 563 times

-1

I’m trying to pass an array of mine view for a controller (it will generate a pdf) in Laravel. I know there’s a chance I could do it this way:

<form class="form-horizontal" method="get" action={{route('pdf.relatorioPessoa',$dataform['nom_nome'],$dataform['nom_endereco'],$dataform['nom_cidade'])}}>
   <button type="submit">Gerar Relatório</button>
</form>

Meanwhile this array is huge, has at least 20 fields. I would have to create a parameter for each of them in the route, plus validate in the controller if they can be null. There’s no way I can pass the array all along the route?

I’ve tried changing the route part to:

{{route('pdf.relatorioPessoa',[$dataform])}}

And it turns out: Errorexception: Array to string Conversion

And without the [] says I need to pass a parameter as if nothing is passing.

  • How are you receiving the parameter in controller?

  • Just like this in the controller: public Function reportPessoa($array){}, already on the route is Route::get('/pdf/person/{array}','pdfController@reportPessoa')->name('pdf.reportPessoa');

  • And if you use the request? https://laravel.com/docs/8.x/requests

  • I tried, but the command does not reach the control. The error occurs in the parameter passage. These are the last two errors cited, conversion from array to string or just talk that has no parameter. I think maybe there is no way to pass an integer array per parameter even...

3 answers

0

It is possible to transform the array on an object and pass it.

// Exemplo em JavaScript

var arraySelecionados = {selected: [1, 2, 3, 4]};

axios.post('nome/da/minha/rota', arraySelecionados) ...

Setting the route something like:

Route::post('nome/da/minha/rota', 'MinhaController@funcao');

And your Controller:

public function funcao(Request $request){
        $variavel = $request->input('selected');
        // [1, 2, 3, 4]
}
  • In case you showed a Javascript variable, and to pass my $dataform variable? I tried to pass Xios.post('pdf.reportPessoa', {$dataform}}); and gave error that expects a Sring and arrived an array. Error code: htmlspecialchars() expects Parameter 1 to be string, array Given

  • Your $dataform is an object? It needs to be for example $dataform = ['selected' => [1, 2, 3].

0

I believe the simplest would be something like:

@php
    $route = route('pdf.relatorioPessoa');

    $nome = $dataform['nom_nome'];
    $endereco = $dataform['nom_endereço'];
    $cidade = $dataform['nom_cidade'];
 
    $query_string = "nome=$nome&endereco=$endereco&cidade=$cidade";

    $action = "{$route}?{$query_string}";
@endphp

<form 
   class="form-horizontal"
   method="GET"
   action={{ $action }}
>

  • Just an informative comment: To generate a report, it is best to send only the entity id and in the controller to complete the report construction. The data you are trying to send is only a good idea when it is data to perform backend filters.

0

I figured out how to send a array integer per parameter in a simple personal way. You need to use this syntax

['dataform'=>$dataform]<!--O primeiro é o nome que você quer dar para variável(ela vai assim no controller) o segundo é o array-->

You also need to change the form method with @method('get') and don’t put the get method directly. The whole code goes like this:

<form class="form-horizontal" method="post" action={{route('pdf.relatorioPessoa',['dataform'=>$dataform])}}>
    @method('get')
    <button type="submit">Gerar Relatório</button>
</form>

The route is still declared as any(to accept get and post method), but with get method only also works!

Route::any('/pdf/pessoa','pdfController@relatorioPessoa')->name('pdf.relatorioPessoa');

I hope you help people with this doubt in the future!

Browser other questions tagged

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