簡體   English   中英

如何根據特定類別對產品進行分頁 laravel-7

[英]How can I paginate products based on specific category 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

您在您的類別而不是產品上添加分頁。 請嘗試此代碼

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

正確的方式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM