【问题标题】:How can I paginate products based on specific category laravel-7如何根据特定类别对产品进行分页 laravel-7
【发布时间】:2020-08-10 14:42:24
【问题描述】:

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

方法 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

【问题讨论】:

  • 能否提供Category类文件,让我们看看?
  • 我有两个表类别,具有多对多关系的产品和数据透视表 category_product。
  • belongsToMany('App\Category'); } }
  • belongsToMany('App\Product'); } }

标签: php laravel web-applications laravel-7 web-development-server


【解决方案1】:

您在类别而不是产品上添加分页。 请尝试此代码

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

【讨论】:

  • 我试试这个它显示了另一个分页不存在的错误
  • 哦,对不起,我现在修好了。我将 () 添加到产品中
  • $products = $category-&gt;products()-&gt;paginate(15);
  • 它有效!非常感谢。
  • 不客气。请接受答案,其他用户可以更容易地找到这个问题的答案
【解决方案2】:

正确的方法:

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

【讨论】:

  • 还是不明白,我想在一个分类中显示所有产品并对产品进行分页。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 2019-09-03
相关资源
最近更新 更多