I’m trying to create a simple filter system with a search box, service, category and a way to sort the items in my laravel controller. However, I am getting an error links() when I try to filter anything even though I think I have paginated correctly. Here’s my function:
public function Search(Request $request){ $search_service=$request->service; $search_order=$request->order; $search_category=$request->category; $search_text=$request->searchModel; $items=Item::where(function($query)use($search_text){ $query->where('model_name','like',"%{$search_text}%") ->orWhere('description','like',"%{$search_text}%") ->orWhere('model_tags','like',"%{$search_text}%");}); if ($search_service){ $items->where('service','=',$search_service); } if($search_category){ $items->where('category','=',$search_category); } if($search_order){ if($search_order =="description" or $search_order=="model_name" or $search_order=="version" or $search_order=="created_at"){ $items->orderBy($search_order); } else if($search_order=="versionDesc"){ $items->orderBy('version','desc'); } else if($search_order=="createDesc"){ $items->orderBy('created_at','desc'); } } $items->paginate(5)->withQueryString(); return view('admin.item.search',compact('items')); }
Can anyone give me some advice on this problem? Thanks!
Edit:
Here is my search page:
@extends('admin.admin_master') @section('admin') {{-- Not placed in app.css as it affects the scroll bar --}} <style> .formForService { text-align: center; } .searchForm{ margin: 0 auto; width:250px; } #show_image_popup{ width: 1200px; height: 700px; border: 1px solid #333; box-sizing: border-box; padding: 5px; text-align: center; overflow-y:scroll; position: fixed; top: 55%; left: 55%; transform: translate(-50%, -50%); background: #e5e5e5; /* */ display: none; } #show_image_popup img{ max-width: 90%; height: auto; } .clickModel{ display: block; margin-left: auto; margin-right: auto; width:1100px; height:670px; } </style> <div class="content-wrapper"> <div class="container-full"> <!-- Content Header (Page header) --> <!-- Main content --> <section class="content"> <div class="row"> <div class="col-12"> <div class="box"> <div class="box-header with-border"> <h3 class="box-title">All Models</h3> <a href='{{ route('add.models') }}' class="btn btn-rounded btn-success mb-5"style="float:right">Add model</a> </div> <!-- /.box-header --> <div class="table-responsive"> <table class="table table-bordered table-striped"> <thead> <tr></tr> <th width="5%">No.</th> <th>Name</th> <th width="10%">Image</th> <th>Description</th> <th>Version</th> <th>Created at</th> <th width="8%">Modify</th> </tr> </thead> <tbody> @if($items) @foreach($items as $key=>$item) <tr> <th scope="row">{{ $items->firstItem()+$loop->index}}</th> <td>{{ $item->model_name }}</td> <td><model-viewer class="small-image" src="{{ asset($item->model_image) }}"alt=""auto-rotate="" camera-controls="" background-color="#455A64"></model-viewer></td> <td>{{ $item->description }}</td> <td>{{ $item->version }}</td> <td> @if($item->created_at == NULL) <span class="text-danger"> No Date Set </span> @else {{ CarbonCarbon::parse($item->created_at)->diffForHumans() }} {{-- Carbon needed for query builder --}} @endif </td> <td> <a href="{{ url('model/download/'.$item->id) }}"class="btn btn-app btn-success"><i class="fa fa-save"></i>Download</a> <a href="{{ url('model/view/'.$item->model_name) }}"class="btn btn-app btn-primary"><i class="fa fa-inbox"></i>View</a> <a href="{{ url('model/edit/'.$item->id) }}"class="btn btn-app btn-info" ><i class="fa fa-edit"></i>Edit</a> </td> </tr> @endforeach @else <td>No records found</td> @endif </tbody> </table> {{ $items->links() }} </div> </div> <!-- /.box --> <!-- /.box --> </div> <!-- /.col --> </div> <!-- /.row --> </section> <!-- /.content --> </div> </div> </div> <div id="show_image_popup"> <div class="close-btn-area"> <button type="button" style="float:right" class="btn btn-circle btn-danger btn-lg mb-5" id="close-btn">X</button> </div> <div id="image-show-area"> <model-viewer class="clickModel" id="large-image" alt=""auto-rotate="" camera-controls="" background-color="#455A64"></model-viewer> </div> </div> @endsection
Advertisement
Answer
You need to assign the value :
$items = $items->paginate(5)->withQueryString();
if you don’t do that your paginator instance is lost and you end up with a builder in the view which has no links method