【问题标题】:Pagination doesn't work after doing a search Laravel搜索 Laravel 后分页不起作用
【发布时间】:2015-05-15 12:34:43
【问题描述】:

您好,当我搜索订单时,我无法将订单分页(每页 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'));

}

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,但这不起作用。

有人可以帮帮我吗?

【问题讨论】:

    标签: php laravel pagination


    【解决方案1】:

    您不能使用 laravel 分页器对原始查询进行分页。您只能对使用查询构建器或 Eloquent 模型进行的查询进行分页。

    您必须手动对查询进行分页(在查询中使用 LIMIT),然后使用 Paginator::Make() 来显示分页器视图。此外,您必须使用不同的查询检索项目数,因为在结果数组上使用 count() 会为您提供该页面中的结果数(通常是页面大小,最后一页除外)

    其他选项是将原始查询更改为使用查询构建器进行的查询,但 laravel 不建议这样做,因为您使用的是“分组依据”,官方文档说:

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

    Official Doc

    【讨论】:

    • 啊,好吧,我已经在上面的代码中使用了 paginator::make()。我已将我的查询编辑为:链接:pastebin.com/uY1maxxC 如您所见,我在查询中添加了“LIMIT”。但我不知道在我的查询中在哪里使用 count() 。请问您或其他人可以帮帮我吗?
    【解决方案2】:

    这就是我在每次分页搜索中所做的:
    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);
    

    查看

    <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() !!}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多