搜索拉拉维尔后分页不起作用


Pagination doesn't work after doing a search Laravel

嗨,当我搜索订单时,我无法对订单进行分页(每页 5 个订单)。

我还记录了自己,向您展示我目前遇到的问题:

链接: https://www.youtube.com/watch?v=sffTI6adS7A&feature=youtu.be

这是我正在使用的代码:

订单控制器.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() 显示分页器视图。此外,您必须使用不同的查询检索项目数,因为在结果数组上使用 count() 会为您提供该页面中的结果数(通常是页面大小,最后一页除外)

另一种选择是从该原始查询更改为使用查询构建器进行的查询,但 laravel 不建议这样做,因为您使用的是"分组依据",官方文档说:

注意:目前,Laravel无法有效地执行使用groupBy语句的分页操作。如果需要将 groupBy 与分页结果集一起使用,建议您手动查询数据库并使用 Paginator::make。

官方文档

这是我

在每个分页搜索中所做的:
控制器

$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 &amp; 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() !!}