Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据特定类别laravel-7对产品进行分页_Php_Laravel_Web Applications_Laravel 7_Web Development Server - Fatal编程技术网

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

Php 如何根据特定类别laravel-7对产品进行分页,php,laravel,web-applications,laravel-7,web-development-server,Php,Laravel,Web Applications,Laravel 7,Web Development Server,我正在尝试查看和检索某个类别中的所有产品,并将其分页。 我能够获得任何类别内的所有产品,它工作正常,但当我尝试对它们进行分页时,它会抛出以下错误: 方法Illumb\Database\Eloquent\Collection::链接不存在。(视图:C:\xampp\htdocs\E-Commerce\resources\views\pages\products.blade.php) 这是我的密码 public function show(Category $category, Request $r

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

方法Illumb\Database\Eloquent\Collection::链接不存在。(视图: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
@extends('layouts/default')
@章节(“标题”、“产品”)
@节(“内容”)
@forelse($categories作为$categories)
{{$category->name}
@foreach($category->products as$product)
@endforeach
{{$category->products->links()}
@空的
现在还没有分类!
@endforelse
@端部
正确的方法:

<?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]);
    }
}
以及结果视图:

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

@foreach($users作为$user)
{{$user->name}
@endforeach
{{$users->links()}
正确的方法:

<?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]);
    }
}
以及结果视图:

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

@foreach($users作为$user)
{{$user->name}
@endforeach
{{$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
@extends('layouts/default')
@章节(“标题”、“产品”)
@节(“内容”)
{{$category->name}
@foreach($products as$product)
@endforeach
{{$products->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
@extends('layouts/default')
@章节(“标题”、“产品”)
@节(“内容”)
{{$category->name}
@foreach($products as$product)
@endforeach
{{$products->links()}
@端部

你能提供类别类文件吗,我们可以看到?我有两个表类别,多对多关系的产品和透视表类别\u产品。你能提供类别类文件吗,我们可以看到?我有两个表类别,多对多关系的产品和透视表类别\u产品。我仍然不明白,我想显示单个类别内的所有产品并对产品进行分页。我仍然不明白,我想显示单个类别内的所有产品并对产品进行分页。我尝试了此操作。它显示另一个错误,即分页不存在。抱歉,我现在已修复。我将()添加到产品
$products=$category->products()->paginate(15)它工作!非常感谢,不客气。请接受其他用户的答案,以便更容易找到此问题的答案。我尝试此操作,它显示另一个错误,分页不存在。抱歉,我现在已修复。我将()添加到产品
$products=$category->products()->paginate(15)它工作!非常感谢,不客气。请接受其他用户的答案,以便更容易找到此问题的答案