【问题标题】:Laravel 5 paginationLaravel 5分页
【发布时间】:2017-11-30 08:44:03
【问题描述】:

我在指南中进行了分页。我在 db 中进行搜索,并使用数据 ti 视图引用变量。

public function search(Request $request)
{
    $products = DB::table('product')->where('number', 'like', $request->number.'%')->paginate(5);
    return view('site.content_layouts', [
        'products' => $products
    ]);
}

这是我的 html 文件的一部分

 @foreach($products as $product)
                <form role="form" method="get" action="{{ route('add_to_cart') }}">
                <tr>
                    <input type="hidden" name = "id" value="{{$product->id}}">

                    <td>{{$product->manufacturer}}</td>
                    <input type="hidden" name = "manufacturer" value="{{$product->manufacturer}}">

                    <td>{{$product->number}}</td>
                    <input type="hidden" name = "number" value="{{$product->number}}">

                    <td>{{$product->name}}</td>
                    <input type="hidden" name = "name" value="{{$product->name}}">

                    <td>{{$product->quantity}}</td>
                    <input type="hidden" name = "quantity" value="{{$product->quantity}}">

                    <td>{{$product->incoming_time}}</td>
                    <input type="hidden" name = "trader" value="{{$product->trader}}">

                    <td>{{$product->price}}</td>
                    <input type="hidden" name = "price" value="{{$product->price}}">

                    <td><input size="5px" type="text"  id="quantity_choose" name="quantity_choose" value="1"></td>

                    <td>
                        <p data-placement="top" data-toggle="tooltip" title="AddToCart">
                            <button class="btn btn-danger btn-xs" data-title="AddToCart" data-toggle="modal" data-target="#AddToCart" >
                                <span class="glyphicon glyphicon-trash">

                                </span>
                                </button>
                        </p>
                    </td>

                </tr>
                </form>
            @endforeach
            </tbody>
        </table>{{$products->links()}}

首页正常显示。但是当我关注其他页面的链接时,显示了我所有数据库的前 5 行,并创建了指向 61k 个页面的链接。不仅行我在第一页搜索的内容 - 它推断出我所有的数据库。请帮帮我)哪里有问题?

1)First page, where al is normally 2)Here I use link of second page

【问题讨论】:

  • 第 2 页的 URL 中是否还有 $request-&gt;number
  • 分页中如何请求页码?
  • 其 1 页的 url - h ttp://shop/search?_token=hqmlTOxVeGnY3kbfpYPz0GPFyiMjNeiemvOIY2TY&number=j1 其 2+ 页的 url - shop/search?page=2
  • @PankajMakwana 我有一个输入表单,我在其中输入了请求编号。

标签: php database laravel laravel-5 pagination


【解决方案1】:

如果你的分页有额外的参数,你应该使用appends()。它将向您的 url 添加更多 GET 参数,因此搜索查询将被传递到第二页中的$request。参考detail here

您的代码应如下所示。

$products = DB::table('product')->where('number', 'like', $request->number.'%')->paginate(5)->appends($request->all());

以上代码将携带所有参数传递给$request

【讨论】:

  • 非常感谢)
猜你喜欢
  • 1970-01-01
  • 2017-01-29
  • 2016-11-17
  • 2015-09-05
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
相关资源
最近更新 更多