首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel分页-调用未定义的方法links\Database\Eloquent\Builder::links()

Laravel分页-调用未定义的方法links\Database\Eloquent\Builder::links()
EN

Stack Overflow用户
提问于 2021-10-22 20:17:24
回答 2查看 82关注 0票数 0

我使用的是Laravel Framework 8.62.0PHP 7.4.20

我得到以下错误:

代码语言:javascript
复制
Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: /home//Code/test_project/resources/views/index.blade.php)

我有一个视图,它使用了3个简单的过滤器。要通过get显示视图,我使用以下命令:

代码语言:javascript
复制
    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);
        }
    }

要过滤我使用的视图:

代码语言:javascript
复制
   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);
        }
    }

在我的web.php中,我有两个端点:

代码语言:javascript
复制
Route::get('/',  [SearchController::class, 'getSearchView'])->name('/');
Route::post('postFilter',  [SearchController::class, 'postFilter']);

在我看来,我使用了laravel的分页:

代码语言:javascript
复制
                                {!! $items->links('vendor.pagination.default') !!}

有什么建议,为什么我会得到上述错误和如何修复它?

感谢您的回复!

EN

回答 2

Stack Overflow用户

发布于 2021-10-24 20:31:23

$items当前是一个查询构建器实例。此对象不会更改,它将继续作为Query Builder实例。当您从查询构建器执行查询时,您会得到一个返回的结果,这就是您需要传递给视图的结果。您可以轻松地将$items重新分配给此结果:

代码语言:javascript
复制
$items = $items->limit(500)->paginate(10);

现在$items是分页器实例,因为您将该变量重新赋值给了paginate调用的结果。

票数 2
EN

Stack Overflow用户

发布于 2021-10-22 20:36:27

代码语言:javascript
复制
public function boot()
{
    Paginator::defaultView('view-name');

    Paginator::defaultSimpleView('view-name');
}

将此代码添加到AppServiceProvider。我希望它能起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69682648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档