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

How can I paginate products based on specific category laravel-7

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

Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: 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

正确的方法:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    public function index()
    {
        $users = DB::table('users')->paginate(15);
        return view('user.index', ['users' => $users]);
    }
}

你也可以使用简单分页:

$users = User::where('votes', '>', 100)->simplePaginate(15);

结果视图:

<div class="container">
    @foreach ($users as $user)
    {{ $user->name }}
    @endforeach
</div>
{{ $users->links() }}

您在您的类别而不是产品上添加分页。 请试试这个代码

public function show(Category $category, Request $request)
{
    $category = Category::where('id', $request->category->id)->first();
    $products = $category->products()->paginate(15);
     
    return view('pages.products')->with([
    'products' => $products,
    'category' => $category'

]);

在你看来

@extends('layouts/default')

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

        <h1 class="text-center">{{ $category->name }}</h1>
        <div class="row mt-5 m-5">
            @foreach ($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   
            {{  $products->links()  }}
        </div>
    

@endsection