简体   繁体   English

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

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

I am trying to view retrieve all products in a category and also paginate them into pages.我正在尝试查看检索类别中的所有产品并将它们分页到页面中。
I am be able to get all products inside any category and it works fine but when I try to paginate them it throws this error:我能够获得任何类别中的所有产品,并且工作正常,但是当我尝试对它们进行分页时,它会引发此错误:

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

Here is my code.这是我的代码。

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

And Here is my view.这是我的观点。

@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

You add paginate on your categories NOT on products.您在您的类别而不是产品上添加分页。 Please try this code请尝试此代码

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'

]);

And in your view在你看来

@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

The right way:正确的方式:

<?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]);
    }
}

Also you can use simplepagination:您也可以使用简单分页:

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

And the result view:结果视图:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何列出 Laravel 类别中的产品? - How can I list the products in the category with Laravel? 使用 Laravel 如何获得特定尺寸的产品类别? - Using Laravel How can get a specific sizes products for a category? 如何获得特定类别“正在销售”的产品数量? - How can I get the number of products “on-sale” for a specific category? 如何在 Laravel 5 中对合并的集合进行分页? - How can I paginate a merged collection in Laravel 5? laravel 如何对单个类别的帖子进行分页 - laravel how to paginate single category posts 我如何使用php从woocommerce restapi获取特定类别的产品 - how can i get products for specific category from woocommerce restapi using php 如何在laravel中对特定列进行分页? - How to paginate a specific column in laravel? 如何按类别分页并转到特定记录? - How to paginate by category and go to a specific record? 在 laravel 中编辑类别时,如何显示特定类别的先前值? - how can i show the previous value of a specific category while editing a category in laravel? 如何仅获取基于 WooCommerce 中特定类别的简单产品和产品变体(不包括可变产品)? - How to get only simple products and product variations (variable products excluded) based on a specific category in WooCommerce?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM