Laravel 分页不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44277240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:03:50  来源:igfitidea点击:

Laravel Paginate not working

phplaravelpagination

提问by User57

I was trying to paginate some data with Query builder with the following query.

我试图通过以下查询使用查询构建器对一些数据进行分页。

public function productSearch(Request $request)
{
     $name = $request->name; 
     $products = DB::table('products')                  
         ->where('name', 'LIKE', "%$name%")             
         ->paginate(4);

     return view('product',compact('products');
}

But when I tried to show the pagination in the view page with the following line

但是当我尝试使用以下行在视图页面中显示分页时

{{ $products->links() }}

It shows

表明

Method links does not exist

方法链接不存在

What could the possible error be since the pagination does not show?

由于分页未显示,可能的错误是什么?

回答by Prabal Thakur

Here is your solution. Just change your return view option

这是您的解决方案。 只需更改您的返回视图选项

return view('product')->with('products', $products);

Hope this helps you.

希望这对你有帮助。

回答by Shubham Gupta

2 types to print pagination link in laravel - Try with -

在 Laravel 中打印分页链接的 2 种类型 - 尝试 -

use Illuminate\Support\Facades\DB;
public function productSearch(Request $request)
{
     $name = $request->name; 
     $products = DB::table('products')                  
         ->where('name', 'LIKE', "%$name%")             
         ->paginate(4);

     return view('product',['products' => $products]);
}

In View -

在视图中 -

<div class="container">
    @foreach($products as $product)
        <h4>{{ $product['name'] }}</h5> <br>
    @endforeach
</div>

1st type to defined laravel pagination link -

定义 Laravel 分页链接的第一种类型 -

   {{ $products->links() }}

And 2nd type to defined laravel pagination link -

第二种定义 Laravel 分页链接的类型 -

   {{ $products->render(); }}

Note - Also you missed in your code -> return view('product',compact('products')); in return view('product',compact('products'); Desired output -

注意 - 你也错过了你的代码 -> return view('product',compact('products') ); in return view('product',compact('products'); 所需的输出 -

回答by Rahul

Change to :

改成 :

return view('product',compact('products'));

In View page try :

在查看页面尝试:

{{$products->render()}}

回答by Roland Allla

In your Controller:

在您的控制器中:

return view('product',compact('products');

In your View:

在您的视图中:

@foreach($products as $product)
  {{ $product->name }}
@endforach

{{ $products->links() }} 

回答by bafdurango

try with:

尝试:

{{ $products->render() }}