嗨,当我正在搜索订单时,我无法将订单分页(每页5个订单)..
我还记录了自己,以向您展示我目前遇到的问题:
link:https://www.youtube.com/watch?v=sffTI6adS7A&feature=youtu.be
这是我正在使用的代码:
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;
}
我尝试了什么:
我已将表单操作更改为GET和POST,但这不起作用。
有人可以帮助我吗?
答案 0 :(得分:2)
您无法使用laravel paginator对原始查询进行分页。您只能对使用查询构建器或Eloquent模型进行的查询进行分页。
您必须手动对查询进行分页(在查询中使用LIMIT),然后使用Paginator :: Make()来显示分页器视图。此外,您必须检索具有不同查询的项目数,因为在结果数组上使用count()将为您提供该页面中的结果数(通常,页面大小,最后一页除外)
其他选项是从原始查询更改为使用查询构建器进行的查询,但是laravel并不建议您使用" Group By"官方文件说:
注意:目前,Laravel无法有效执行使用groupBy语句的分页操作。如果您需要将groupBy与分页结果集一起使用,建议您手动查询数据库并使用Paginator :: make。
答案 1 :(得分:2)
这是我在每个分页搜索中所做的事情:
的控制器强>
$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);
查看强>
<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() !!}