您好,当我搜索订单时,我无法对订单进行分页(每页 5 个订单)..
我也记录了自己的情况,以向您展示我目前遇到的问题:
链接: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'));
}
订单.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,但这不起作用。
有人可以帮助我吗?
最佳答案
您无法使用 laravel 分页器对原始查询进行分页。您只能对使用查询生成器或 Eloquent 模型进行的查询进行分页。
您必须手动对查询进行分页(在查询中使用 LIMIT),然后使用 Paginator::Make() 显示分页器 View 。此外,您还必须使用不同的查询检索项目数,因为在结果数组上使用 count() 将为您提供该页面中的结果数(通常是页面大小,最后一页除外)
其他选项是从原始查询更改为使用查询生成器进行的查询,但 laravel 不建议这样做,因为您正在使用“Group By”并且官方文档说:
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.
关于php - Laravel 搜索后分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28057259/