Agniaa
Agniaa

Reputation: 11

Laravel get route view without redirect

I am making a search in website.

public function search(Request $request)
{ 
  $search_value = $request->search_txt;

  $data_res = Data::Where('text', 'like', '%' . $search_value . '%')->get();
  $navbar_search = Navbar::where('id','183')->first();
  return redirect(route('response', $navbar_search->slug))->with('data_res',$data_res);
}

This is my controller function. I'm getting problem that i want to display data in the same page. I need to return view to this exact 'response' route and send slug. This redirect does not work, because $data_res after redirect is empty..

Routes:

Route::post('/search/search_res', 'SearchController@search')->name('search.srch');
Route::get('/{slug}', 'FrontEndPagesController@index')->name('response');

HTML:

<form class="form-horizontal" method="POST" action="{{ route('search.srch') }}">
  {{ csrf_field() }}

  <div class="main search_field">
    <div class="input-group">
      <input type="search" name="search_txt" class="form-control" value="{{ old('search_txt') }}" placeholder=" tekstas apie paieska ?" required maxlength="200" style="padding-left: 10px !important;">
      <div class="input-group-append">
        <button class="btn btn-secondary" type="submit">
          <i class="fa fa-search"></i>
        </button>
      </div>
    </div>
  </div>
</form>

@if(!empty($data_res))
  @foreach($data_res as $data)
    {{ $data->id }}
  @endforeach
@endif

Upvotes: 0

Views: 1071

Answers (1)

Firdaus Nasir
Firdaus Nasir

Reputation: 72

With my little experience, I think when you redirect to another route and includes 'with', it stores the data in session.

you may want to access in the function FrontEndPagesController@index by

 $data_res = session()->get('data_res');

then, to make it available for blade file, put it in return response like

return view('your response view', compact('data_res')

Upvotes: 1

Related Questions