[英]Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links()
I am using Laravel Framework 8.62.0
and PHP 7.4.20
.我正在使用Laravel Framework 8.62.0
和PHP 7.4.20
。
I get the following error:我收到以下错误:
Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: /home//Code/test_project/resources/views/index.blade.php)
I have a view that has uses 3 simple filters.我有一个使用 3 个简单过滤器的视图。 To display the view via get I use the following:要通过 get 显示视图,我使用以下命令:
public function getSearchView()
{
try {
$con = 'mysql_prod';
// search results
$items = Item::on($con)->select(['items.name AS item_name', 'items.slug', 'items.id AS item_id', 'item_details.sticker_number', 'item_details.section', 'item_details.type', 'collections.name AS collections_name', 'collections.sport_type', 'collections.league', 'collections.year as collections_year', 'images.file_name'])
->leftJoin('item_details', 'items.id', '=', 'item_details.items_id')
->leftJoin('collections', 'items.collections_id', '=', 'collections.id')
->leftJoin('images', 'images.items_id', '=', 'items.id')
->limit(500)
->paginate(10);
// filter field
$condition = Condition::on($con)->select(['id', 'name AS condition_name'])
->distinct()
->get();
$collection = Collection::on($con)->select(['id', 'name AS collection_name'])
->distinct()
->orderBy('collection_name', 'ASC')
->get();
return view('index', compact('items'));
} catch (\Exception $e) {
Log::error($e);
report($e);
}
}
To filter the view I use:要过滤我使用的视图:
public function postFilter(Request $request)
{
try {
$con = 'mysql_prod';
//##################################
// QUERY - SEARCH RESULTS
//##################################
$items = Item::on($con)->select(['items.name AS item_name', 'items.slug', 'items.id AS item_id', 'item_details.sticker_number', 'item_details.section', 'item_details.type', 'collections.name AS collections_name', 'collections.sport_type', 'collections.league', 'collections.year as collections_year', 'images.file_name'])
->leftJoin('item_details', 'items.id', '=', 'item_details.items_id')
->leftJoin('collections', 'items.collections_id', '=', 'collections.id')
->leftJoin('images', 'images.items_id', '=', 'items.id');
// collection
if(!is_null($request->select_collection_field)) $items->where('collections.id', '=', intval($request->select_collection_field));
// FILTER field
if(!is_null($request->select_filter_field)) {
if($request->select_filter_field === "select_all") $items->orderBy('item_name', 'desc');
if($request->select_filter_field === "publishing_year") $items->orderBy('collections_year', 'desc');
}
// query database
$items->limit(500)->paginate(10);
//##################################
// FILTERS
//##################################
$condition = Condition::on($con)->select(['id', 'name AS condition_name'])
->distinct()
->get();
$collection = Collection::on($con)->select(['id', 'name AS collection_name'])
->distinct()
->orderBy('collection_name', 'ASC')
->get();
return view('index', compact('items', 'condition', 'collection'));
} catch (\Exception $e) {
Log::error($e);
report($e);
}
}
In my web.php
I have the two endpoints:在我的web.php
我有两个端点:
Route::get('/', [SearchController::class, 'getSearchView'])->name('/');
Route::post('postFilter', [SearchController::class, 'postFilter']);
In my view I use the pagination of laravel:在我看来,我使用 laravel 的分页:
{!! $items->links('vendor.pagination.default') !!}
Any suggestions why I get the above error and how to fix it?任何建议为什么我会收到上述错误以及如何解决它?
I appreciate your replies!我感谢您的回复!
public function boot()
{
Paginator::defaultView('view-name');
Paginator::defaultSimpleView('view-name');
}
add this code to AppServiceProvider.将此代码添加到 AppServiceProvider。 I hope it will work.我希望它会起作用。
$items
is currently a Query Builder instance. $items
当前是一个查询生成器实例。 This object wont change, it will continue to be a Query Builder instance.这个对象不会改变,它将继续是一个 Query Builder 实例。 When you execute a query from a Query Builder you get a returned result, and that is what you need to be passing to your view.当您从 Query Builder 执行查询时,您会得到一个返回的结果,这就是您需要传递给您的视图的结果。 You could reassign $items
to this result easily:您可以轻松地将$items
重新分配给此结果:
$items = $items->limit(500)->paginate(10);
Now $items
is the Paginator instance because you reassigned that variable to the result of the paginate
call.现在$items
是 Paginator 实例,因为您将该变量重新分配给paginate
调用的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.