Reputation: 1279
Hi when I'm doing a search for a order, then I can't get my orders paginated (5 orders per page)..
I have also recorded myself to show you the problem that I currently have:
link: https://www.youtube.com/watch?v=sffTI6adS7A&feature=youtu.be
This is the code that I'm using:
OrdersController.php:
public function post_search()
{
$keyword=Input::get('keyword');
if(empty($keyword))
return Redirect::route('user.orders.index')->with('error_search', 'Er was geen zoekterm ingevuld, probeer het nog eens.');
//$orders = Order::with('client')->get();
//$orders= new stdClass;
//$orders->foo=Order::search($keyword)->paginate(5);
$orders=Order::search($keyword);
$msg="";
if (!count($orders)) {
$msg="There were no orders found.";
}else{
$msg="There were orders found";
}
$orders = Paginator::make($orders, count($orders), 5);
foreach( $orders as &$order){
//we initialize ordertask
$order->ordertask = Ordertask::where('id_order', '=', $order->id)->get();
}
return View::make('user.orders.index', compact('orders', 'msg'));
}
Order.php:
public static function search($keyword){
DB::connection()->getPdo()->quote($keyword);
$result = DB::Select(DB::raw('SELECT orders.*, tasks_x.task_all
FROM orders LEFT JOIN (SELECT GROUP_CONCAT(tasks.task_name SEPARATOR ",")
AS task_all, ordertasks.id_order
FROM tasks JOIN ordertasks
on ordertasks.id_task = tasks.id GROUP BY ordertasks.id_order) as tasks_x
ON tasks_x.id_order = orders.id WHERE orders.order_name
LIKE "%'.$keyword.'%" OR tasks_x.task_all LIKE "%'.$keyword.'%"'));
return $result;
}
What have I tried:
I have changed the form action to GET and POST, but that didn't work.
Can someone maybe help me, please?
Upvotes: 2
Views: 3479
Reputation: 1004
This is what I do in every pagination search:
Controller
$query = new Product();
//dynamic search term
if (Input::has('code')){
$term = Input::get('code');
$term = trim($term);
$term = str_replace(" ", "|", $term);
$query = $query->whereRaw("itemcode regexp '".$term."'");
if (Input::has('description')){
$term = Input::get('description');
$term = trim($term);
$term = str_replace(" ", "|", $term);
$query = $query->whereRaw("itemcode regexp '".$term."'");
}
//if there are more search terms, just copy statements above
$products = $query->paginate(50); //get 50 data per page
$products->setPath(''); //in case the page generate '/?' link.
$pagination = $products->appends(array('code' => Input::get('code'),
'description' => Input::get('description')
//add more element if you have more search terms
));
//this will append the url with your search terms
return redirect('product')->with('products', $products)->with('pagination', $pagination);
View
<div class="panel panel-default">
<div class="panel-heading">
<h6 class="panel-title"><i class="icon-paragraph-justify"></i> Striped & bordered datatable</h6>
</div>
<div class="datatable">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>Item Code</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach($products as $key)
<tr>
<td>{{ $key->id}}</td>
<td>{{ $key->itemcode }}</td>
<td>{{ $key->description }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
{!! $products->render() !!}
Upvotes: 2
Reputation: 1079
You can't paginate a raw query using laravel paginator. You can only paginate queries made with the query builder or an Eloquent model.
You would have to manually paginate the query (using LIMIT in your query), and then use Paginator::Make() to show the paginator view. Also, you would have to retrieve the number of items with a different query, as using count() over the results array would give you the number of results in that page (Generally, the page size, except for last page)
Other option would be to change from that raw query to a query made using the query builder, but laravel doesn't recommends this as you're using "Group By" and the official docs says:
Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database manually and use Paginator::make.
Upvotes: 2