简体   繁体   English

Laravel 8 多对多关系分页

[英]Laravel 8 pagination for many to many relation

I have many to many relation for categories and products.我对类别和产品有多对多的关系。 In a specific category page I want to add pagination for products.在特定类别页面中,我想为产品添加分页。

This is my code in Category model:这是我在 model 类别中的代码:

public function products() {
    return $this->belongsToMany(Product::class,
        'category_product',
        'category_id',
        'product_id'
    )
    ->where('is_published',1)
    ->orderBy('id','ASC')->paginate(5);
}

This is CategoryController code:这是 CategoryController 代码:

public function show($id) {       
    $category = Category::findorfail($id);        
    return view(category.show, compact('category'); 
}

This is a pagination code in category view blade:这是类别视图刀片中的分页代码:

{{ $products->links() }}

This is an error I'm getting:这是我得到的错误:

App\Models\Category::products must return a relationship instance.

Any solutions?有什么解决办法吗?

The error message is pretty obvious, The relationship method must return a relationship instance.错误信息很明显,关系方法必须返回一个关系实例。 So these two are gonna work because those return an instance of type Illuminate\Database\Eloquent\Relations\BelongsToMany class:所以这两个会起作用,因为它们返回Illuminate\Database\Eloquent\Relations\BelongsToMany BelongsToMany class 类型的实例:

public function products() {
    return $this->belongsToMany(Product::class,
        'category_product',
        'category_id',
        'product_id'
    );
}

// Or

public function products() {
    return $this->belongsToMany(Product::class,
        'category_product',
        'category_id',
        'product_id'
    )
    // This update the internal query of the relationship instance to query the relationship later on.
    ->where('is_published',1)->orderBy('id','ASC');
}

Your code will return an instance of Illuminate\Pagination\LengthAwarePaginator , which is NOT gonna work:您的代码将返回Illuminate\Pagination\LengthAwarePaginator的一个实例,它不会起作用:

public function products() {
    return $this->belongsToMany(Product::class,
        'category_product',
        'category_id',
        'product_id'
    )
    ->where('is_published',1)
    ->orderBy('id','ASC')->paginate(5);
}

Then in your controller, you can do something like:然后在您的 controller 中,您可以执行以下操作:

class CategoriesController extends Controller
{
    public function show($id)
    {
        $category = Category::findOrFail($id);
        $products = $category->products()->paginate(5);

        return view('categories.show', compact('category', '$products'));
    }
}

Found a solution for this:为此找到了解决方案:

CategoryController:类别控制器:

public function show($id) {       
    $category = Category::findorfail($id);
    $category->setRelation('products', $category->products()->paginate(5));
    
    return view(category.show, compact('category'); 
}

View blade:查看刀片:

@foreach($category->products as $product)
    {{ $product->title }}
@endforeach

{!! $category->products->render() !!}

Category model:类别 model:

public function products() {
    return $this->belongsToMany(Product::class,
        'category_product',
        'category_id',
        'product_id'
    )
    ->where('is_published',1)
    ->orderBy('id','ASC');
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM