簡體   English   中英

Laravel 分頁 - 調用未定義的方法 Illuminate\\Database\\Eloquent\\Builder::links()

[英]Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links()

我正在使用Laravel Framework 8.62.0PHP 7.4.20

我收到以下錯誤:

Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: /home//Code/test_project/resources/views/index.blade.php)

我有一個使用 3 個簡單過濾器的視圖。 要通過 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);
        }
    }

要過濾我使用的視圖:

   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我有兩個端點:

Route::get('/',  [SearchController::class, 'getSearchView'])->name('/');
Route::post('postFilter',  [SearchController::class, 'postFilter']);

在我看來,我使用 laravel 的分頁:

                                {!! $items->links('vendor.pagination.default') !!}

任何建議為什么我會收到上述錯誤以及如何解決它?

我感謝您的回復!

public function boot()
{
    Paginator::defaultView('view-name');

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

將此代碼添加到 AppServiceProvider。 我希望它會起作用。

$items當前是一個查詢生成器實例。 這個對象不會改變,它將繼續是一個 Query Builder 實例。 當您從 Query Builder 執行查詢時,您會得到一個返回的結果,這就是您需要傳遞給您的視圖的結果。 您可以輕松地將$items重新分配給此結果:

$items = $items->limit(500)->paginate(10);

現在$items是 Paginator 實例,因為您將該變量重新分配給paginate調用的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM