[英]Laravel 7.6 Call to undefined method Illuminate\Database\Eloquent\Builder::appends()
I'm not being able to append the result with query.我无法通过查询对结果进行 append 。 I'm filtering the result with category on the page and trying to add pagination.
我正在使用页面上的类别过滤结果并尝试添加分页。 When a user clicks on category only category related products should be displayed and on pagination I want to keep the selected category.
当用户单击类别时,应仅显示与类别相关的产品,并且在分页时我想保留所选类别。 I'm getting this error:
我收到此错误:
Facade\Ignition\Exceptions\ViewException Call to undefined method Illuminate\Database\Eloquent\Builder::appends() (View: D:\Xampp\htdocs\ProjectName\resources\views\products\index.blade.php) Facade\Ignition\Exceptions\ViewException 调用未定义的方法 Illuminate\Database\Eloquent\Builder::appends()(视图:D:\Xampp\htdocs\ProjectName\resources\views\products\index.blade.php)
I have tried this from laravel docs我已经从 laravel 文档中尝试过这个
{{ $users->appends(['category' => 'query'])->links() }}
Here's my Controller:这是我的 Controller:
public function index(Request $request)
{
// If the category is selected
if(request()->category) {
$products = Product::with('categories')->whereHas('categories', function ($query){
$query->where('slug', request()->category);
});
$categories = Category::all();
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
}else {
$products = Product::paginate(9);
$categories = Category::all();
$categoryName = 'All Products';
}
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
This is my blade file:这是我的刀片文件:
<h4>{{ $categoryName }}</h4>
@forelse ($products as $product)
<div class="card mb-3 shadow" style="">
<div class="row no-gutters">
<div class="col-md-4 d-flex justify-content-around p-2 border">
<img src="/storage/products/{{ $product->image }}" class="card-img">
</div>
<div class="col-md-8 p-2">
<div class="card-body">
<h5 class="card-title">{{ $product->title}}</h5>
<p class="card-text"> {!! \Illuminate\Support\Str::limit($product->text, $limit = 100) !!} </p>
<div class=""><a href="/products/{{ $product->slug }}"><button class="btn btn-outline-warning float-left">View Cataloge</button></a>
</div>
</div>
</div>
</div>
</div>
@empty
<div class="card">
<div class="card-title p-5 shadow bg-light">
<h5>No products found</h5>
</div>
</div>
@endforelse
<div class=" d-flex justify-content-around">
{{ $products->appends(request()->input())->links() }}
</div>
Here you are missing paginate(9)
also pass ($request) in closure function在这里,您缺少
paginate(9)
也通过 ($request) 关闭 function
public function index(Request $request)
{
// If the category is selected
if (!empty($request)) {
$products = Product::whereHas('categories', function ($query) use ($request) {
$query->where('slug', $request->category);
})->paginate(9);
$categories = Category::all();
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
else {
$products = Product::paginate(9);
$categories = Category::all();
$categoryName = 'All Products';
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.