【问题标题】:Table pagination trouble on LaravelLaravel 上的表格分页问题
【发布时间】:2018-10-16 06:20:13
【问题描述】:

我在 laravel 的分页上遇到了一些麻烦。已经对表格进行了分页。但在视图页面中,数字不正确。这是它的样子:

https://i.stack.imgur.com/407Oo.png

另一个问题是当我按下数字时,链接失效并给我这个错误:

Symfony\Component\HttpKernel\Exception\ MethodNotAllowedHttpException 无消息

这是我的路线:

Route::get("/", "PagesController@welcome");

Route::post("/search", "PagesController@search")->name('search.route');

这是我的观点:

public function search(Request $request)
    {
        $q = $request->q;
        if ($q !== null && trim($q) !== ""){//here

            $estates = \DB::table('allestates')
                ->where("building_name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orWhere("region","LIKE", "%" . $q . "%")
                ->orderBy('price')->paginate(10);


            if(count($estates) > 0){
                return view("search", compact('estates'))->withQuery($q);
            }

        }

        $estates = array();//here
        return view("search", compact('estates'))->withMessage("No Found!");//here
    }

另一件事是在控制器中我无法将->paginate(10)->get() 添加它作为错误返回,例如:

方法 Illuminate\Support\Collection::paginate 不存在。

我非常感谢任何帮助解决这个问题。谢谢! 顺便说一句,我不知道是否连接,但我已经公开删除/更改了“boostrap”文件。

【问题讨论】:

  • 你可以创建一个新的 LengthAwarePaginator。一般用法如下: $new_paginate = new LengthAwarePaginator($currentItems, count($items), $perPage, $currentPage);

标签: php laravel pagination


【解决方案1】:

你不能使用paginate() 方法到DB 门面。因为DB 返回集合实例。但是paginate()是Model的方法。您需要从集合实例手动创建分页器。试试这个:

$estates = \DB::table('allestates')
                ->where("building_name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orWhere("region","LIKE", "%" . $q . "%")
                ->orderBy('price')->get();

$showPerPage = 10;

$perPagedData = $estates
            ->slice((request()->get('page') - 1) * $showPerPage, $showPerPage)
            ->all();

        $estates = new Illuminate\Pagination\LengthAwarePaginator($perPagedData, count($estates), $showPerPage, request()->get('page'));

来源:Manually Creating A Paginator

【讨论】:

  • 这就是我得到的:Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'App\Http\Controllers\Illuminate\Pagination\LengthAwarePaginator' not found
  • 好的,我通过添加 \ 来修复该错误,但是现在没有数据显示只是一个空白页伴侣!
  • @KubilayTurgut 转到源链接。阅读和做。每一步我都帮不了你。
【解决方案2】:

我解决了问题。

更改了正下方的控制器。

$q = $request->q;
$estates = \DB::table('allestates')
                ->where("building_name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orWhere("region","LIKE", "%" . $q . "%")
                ->orderBy('price')->paginate(10);

return view("search", compact('estates','q'));

改变观点

@if(count($estates))
    @foreach($estates as $estate)
        <!-- ... -->
    @endforeach
    {{ $estates ->appends(['q' => $q])->links() }}
@else
    <div class="alert">
        Not found!
    </div>
@endif

我也改变了下面的路线。

Route::any("/search", "PagesController@search")->name('search.route');

还为分页按钮添加了 css。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2017-11-22
    • 2020-01-02
    • 1970-01-01
    • 2018-09-09
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多