如何根据特定类别对产品进行分页 laravel-7

我正在尝试查看检索类别中的所有产品并将它们分页到页面中。
我能够获取任何类别中的所有产品,并且工作正常,但是当我尝试对它们进行分页时,它会抛出此错误:

方法 Illuminate\Database\Eloquent\Collection::links 不存在。(查看:C:\xampp\htdocs\E-Commerce\resources\views\pages\products.blade.php)。

这是我的代码。

public function show(Category $category, Request $request)

{

    $categories = Category::where('id', $request->category->id)->paginate(12);

    

    return view('pages.products')->with([

    'categories' => $categories,

]);

这是我的观点。


@extends('layouts/default')


@section('title', '| Products')

@section('content')


    @forelse ($categories as $category)

        <h1 class="text-center">{{ $category->name }}</h1>

        <div class="row mt-5 m-5">

            @foreach ($category->products as $product)

                <div class="cols m-4 shadow-lg product product-box"> 

                    <a href="">

                        <h4 class="text-center">{{ $product->name }}</h4>

                        <img class="m-auto" src="{{ asset('images/welcome2.jpg') }}" width="150px">

                        <br>

                        <small class="text-center">{{ $product->details }}</small>

                        <br>

                        <h6 class="float-right mr-1">&dollar; {{ $product->price }}</h6>

                    </a>

                </div>

            @endforeach   

            {{  $category->products->links()  }}

        </div>

    @empty

        <h1>There are no categories yet!</h1>

    @endforelse

    


@endsection


月关宝盒
浏览 99回答 2
2回答

达令说

您可以在类别而不是产品上添加分页。请尝试这个代码public function show(Category $category, Request $request){&nbsp; &nbsp; $category = Category::where('id', $request->category->id)->first();&nbsp; &nbsp; $products = $category->products()->paginate(15);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; return view('pages.products')->with([&nbsp; &nbsp; 'products' => $products,&nbsp; &nbsp; 'category' => $category']);而在你看来@extends('layouts/default')@section('title', '| Products')@section('content')&nbsp; &nbsp; &nbsp; &nbsp; <h1 class="text-center">{{ $category->name }}</h1>&nbsp; &nbsp; &nbsp; &nbsp; <div class="row mt-5 m-5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($products as $product)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="cols m-4 shadow-lg product product-box">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4 class="text-center">{{ $product->name }}</h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="m-auto" src="{{ asset('images/welcome2.jpg') }}" width="150px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <small class="text-center">{{ $product->details }}</small>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6 class="float-right mr-1">&dollar; {{ $product->price }}</h6>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{&nbsp; $products->links()&nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;@endsection

慕的地6264312

正确的方式:<?phpnamespace App\Http\Controllers;use App\Http\Controllers\Controller;use Illuminate\Support\Facades\DB;class UserController extends Controller{&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $users = DB::table('users')->paginate(15);&nbsp; &nbsp; &nbsp; &nbsp; return view('user.index', ['users' => $users]);&nbsp; &nbsp; }}您也可以使用简单分页:$users = User::where('votes', '>', 100)->simplePaginate(15);以及结果视图:<div class="container">&nbsp; &nbsp; @foreach ($users as $user)&nbsp; &nbsp; {{ $user->name }}&nbsp; &nbsp; @endforeach</div>{{ $users->links() }}
打开App,查看更多内容
随时随地看视频慕课网APP