php - Laravel 搜索后分页不起作用

标签 php laravel pagination

您好,当我搜索订单时,我无法对订单进行分页(每页 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.

Official Doc

关于php - Laravel 搜索后分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28057259/

相关文章:

javascript - 如何在 ReactJS 中实现前端分页?

angularjs - 如何告诉 ui-Bootstrap 对哪些内容进行分页?

php - 如何在 Laravel Yajra DataTables 中的关系列上编辑列

php - 尝试使用登录表单中的密码验证数据库中的散列密码

php - 执行方法始终设置为 true

php - 在 laravel 中动态使用模型

javascript - 在 Laravel 5.7 事件监听器中运行 Javascript

php - 如何在自定义验证规则中使用 laravel 的验证规则?

javascript - 内联 React JS For 循环分页

php - 通过php向特定的mysql数据库表输入数据,结果 "undefined index error"指向另一个表的变量