How to pass data searched by a view back to the view itself

Asked

Viewed 1,880 times

2

I need to search a database through a view (Laravel Blade). The data obtained, stored in an array, need to display on the page called the database. I am not succeeding in returning the array with such data, because I don’t know how to handle the array within the view block waiting for this answer.

Explain:

The view that prepares the database query has a form, with a Submit button. This action triggers a Controller method that goes to the database, searches the data and returns an array. The data exists, the array is populated. Are the following steps:

First, the view and the form. Filename: listar.blade.php:

@extends ('PrincipalView')
@corpo
<form name="frmTSPesquisar" 
action="{{    action('TSController@pesquisarExecutar') }}" 
method="GET" role="form">
Busca: <input list="files" placeholder="20000">
<datalist id="files">
<option>20000</option>          
</datalist>         
<button type="submit">Pesquisar</button>
</form>
@stop

Note that the form Action tag already forwards what Submit will cause.

On the Tscontroller.php page, where the triggered method resides, I have the code:

public function pesquisarExecutar(Request $request) {
    $resultado=DB::table('tabts')
            ->join('tabfiles', 'tabts.file_tabfiles', '=', 'tabfiles.id')
            ->where('tabfiles.file',"=",$request->input('namSelNumFile'))
            ->get();

}//pesquisarExecutar

In this above method, how could you make the array $outworking could be used on the same page (list.blade.php) original, below the form that caused this data collection?

The structure of this array $outworking could complete the code with the form in some way like this:

@extends ('PrincipalView')
@corpo
  <form name="frmTSPesquisar" 
action="{{  action('TSController@pesquisarExecutar') }}" method="GET" role="form">
    Busca: <input list="files" placeholder="20000">
    <datalist id="files">
    <option>20000</option>          
    </datalist>         
    <button type="submit">Pesquisar</button>
    </form>
@stop
{{-- abaixo seria o que eu preciso para interagir com a resposta via array --}}
@foreach ($resultado as $r)
{{r$->datalancamento}}
@endforeach

Anyway, my problem is, I believe, how to pass the array to the view and, once in this view, how to loop to take advantage of each value obtained within the array.

1 answer

1


To pass variables to the view in the 5.* window on the return of your controller’s method use:

  return  view('listar',compact('resultado'));

    ou 

  return  view('listar',get_defined_vars());
  • Very useful your comment. I followed what you said and it worked, but changed the passage of the values as follows: Return view('list')->with('result',$result);

  • I edited my answer because when you use Compact the variable should be passed as string

Browser other questions tagged

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