JOHNMAC's avatar

search method in laravel isn't working

hello, the search in Laravel isn't working, nothing shown in results an in developers mode it shows error about pagination which is working fine,

Error:

     found in

  ---> <RenderlessLaravelVuePagination>
         <Pagination>
         <Users> at resources/js/components/Users.vue
         <Root>

search:

   <div class="input-group input-group-sm">
    <input class="form-control form-control-navbar" @keyup="searchit" v-model="search" type="search" 
       placeholder="Search" aria-label="Search">
    <div class="input-group-append">
      <button class="btn btn-navbar" @click="searchit">
        <i class="fa fa-search"></i>
      </button>
    </div>
    </div>

Controller:

  public function search(){

    if ($search = \Request::get('q')) {
        $users = User::where(function($query) use ($search){
            $query->where('name','LIKE',"%$search%")
                    ->orWhere('email','LIKE',"%$search%");
        })->paginate(20);
    }else{
        $users = User::latest()->paginate(5);
    }

    return $users;

  }

Route:

  Route::put('finduser','API\UserController@search');

app.js:

  const app = new Vue({
      el: '#app',
      router,
      data:{
    search: ''
      },
      methods:{
    searchit: _.debounce(() => {
        Fire.$emit('searching');
    },1000),

      }
  });

could anyone suggest the the main problem or solution?

0 likes
1 reply
jlrdw's avatar

Sorry at first I missed it was for an API. Perhaps change category to Vue.

See

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

You have to pass the "filters" in the url or query string.

Also see

https://laracasts.com/discuss/channels/laravel/filter-and-pagination-1

and

https://laracasts.com/discuss/channels/laravel/laravel-5-query-builder-search-filter

$query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);

        $params = array('psch' => $dogsearch, 'aval' => $aval);
        $title = 'Admin';
return view('dog.index', compact('dogs', 'params'))
                        ->with('title', $title);

view:

{{ $dogs->appends($params)->links() }}

Gives

somesite.com/dogs?page=2&psch=b&aval=1

In other words a query string.

If NOT using query string, you "build" parameters in your uri and pass as route parameters.

Jeffrey has video lessons, and there's documentation.

Just example, you should setup query scopes.

Please or to participate in this conversation.