Andreas94's avatar

Problem with the redirect of a search form

Hi guys, I'm stuck in a very stupid problem... I've put a search on my page, But the redirect is incorrect...

The search page is: 127.0.0.1:8000/articolo And when you click "send", You will be redirected to: 127.0.0.1:8000/ricerca/Your+Query+exemple

As I did instead, I'm redirected to: 127.0.0.1:8000/articolo?cerca=your+query+example, Thus having problems as well with pagination since they point to /articolo?page=2...

How do I fix the redirect?

Route:

Route::group(['prefix' => 'ricerca'], function(){
    Route::get('{search}', ['as'=>'search.article','uses'=>'FilterController@getSearch']);
});

Blade:

{!! Form::open(['method'=>'get', 'class'=>'col-12'])  !!}
<div class="input-group custom-search-form">
  <input type="text" class="form-control" name="cerca" placeholder="Search...">
  <span class="input-group-btn">
    <button class="btn btn-default-sm" type="submit">
      <i class="fa fa-search"></i>
    </button>
  </span>
</div>
{!! Form::close() !!}

Controller:

public function getSearch($search) {
    $search = urldecode($search);
    $articoli = Articoli::where('titolo', 'like', '%'.$search.'%')->orwhere('testo', 'like', '%'.$search.'%')->orderBy('created_at', 'desc')->paginate(12);
    return view('front.pages.article.list', compact('articoli'));
}

0 likes
6 replies
jlrdw's avatar

See this and notice the search boxes at top

https://drive.google.com/file/d/0B1_PFw--3o74YjVreHNBOWU2aEE/view

I just have a small form at top of the list page to search like

<div>

    <form method="post" action="<?php echo DIR; ?>dog/indexadmin">
        <label>sch</label><input type="text" name="psch" value="">
        <label>Avail y or n</label><input type="text" name="aval" value="" size="10">
        <input type="submit" name="submit" value="Search">       
    </form>
</div>

The search goes to a route which in turn calls a controller method to perform the search.

Make sure you are appending the querystring see the docs.

Andreas94's avatar

But doing this, I'm brought to the page "dog/indexadmin" example, and not in "dog/indexadmin/{query}"

or am I wrong?

Snapey's avatar

This

{!! Form::open(['method'=>'get', 'class'=>'col-12'])  !!}

You have not defined an action therefore the form will use the same URL as the current page

You are doing GET and not POST. Therefore the data will be added as a query string parameter

I suggest you stick with GET for a search box, but you set the route to ricerca

Also lose the route group;

Route::get('ricerca', 'FilterController@getSearch')->name('search.article')
1 like
Andreas94's avatar

Ok, Thanks to everyone, it works now but I still have a problem with the pagination.

I am in: /ricerca/?q=Grand+theft, And research from its results, unfortunately, however, pagination does not work:

<li><a href="http://127.0.0.1:8000/ricerca?page=3">3</a></li>
<li><a href="http://127.0.0.1:8000/ricerca?page=2">2</a></li>

While instead, pagination should become:

<li><a href="http://127.0.0.1:8000/ricerca?q=Grand+theft?page=3">3</a></li>
<li><a href="http://127.0.0.1:8000/ricerca?q=Grand+theft?page=2">2</a></li>

By changing the page, I obviously get the error "Undefined index: q".

This is my new code

Controller:
    public function getSearch(Request $request)
    {
      $id = $request->input('q');
      $query = $request->all();
        
    $articoli = Articoli::where('titolo', 'like', '%'.$query['q'].'%')->orwhere('testo', 'like', '%'.$query['q'].'%')->orderBy('created_at', 'desc')->paginate(12);
    return view('front.pages.article.list', compact('articoli'));
    }

Route:

Route::get('ricerca', 'FilterController@getSearch')->name('search.article');

Form Blade:

        <form method="get" action="{{url('ricerca/')}}" role="search">
            <div class="input-group custom-search-form">
              <input type="text" class="form-control" name="q" placeholder="Search...">
              <span class="input-group-btn">
                <button class="btn btn-default-sm" type="submit">
                  <i class="fa fa-search"></i>
                </button>
              </span>
            </div>
        </form>
Andreas94's avatar

Resolved with:

{{ $articoli->appends($_GET)->links() }}

Please or to participate in this conversation.