【问题标题】:Custom Bootstrap Pagination in Laravel errorLaravel 错误中的自定义引导分页
【发布时间】:2018-09-02 19:36:17
【问题描述】:

我关注网站上的文档 https://medium.com/appstract/custom-bootstrap-pagination-in-laravel-540922f71af0

但我有两个问题: 这是我的代码

Route::get(‘rows’, ‘ComeController@rows’);

第一:

public function rows(Request $request)
{
$search_cons=$request->all();
$tablename=$search_cons[‘tablename’];
$search_alls = DB::table($tablename)
->where(‘product’,’=’,’5');

(1.)
    ->get();
    ?//it shows Method links does not exist

(2).
    ->>paginate(9);
    //it will ocupy the $perPage=9  when I use dd($search_alls)



   $page =   $request->get(‘page’, 1);
    $perPage = 6;
    $items = collect($myItems);
    return view(‘my.view’, [ ‘items’ => $items->forPage($page, $perPage), ‘pagination’ => BootstrapComponents::pagination($items, $page, $perPage, ‘’, [‘arrows’ => true]) ]);
}

第二:

when click the page link to the page2 just like http://…/rows?page=2
it shows Undefined index: tablename

我使用 ->page(9) 吗?但是 $perpage 缺少它的功能

LengthAwarePaginator {#279 ▼
  #total: 13
  #lastPage: 2
  #items: Collection {#262 ▶}
  #perPage: 9
  #currentPage: 1
  #path: "http://localhost:8000/comefoshowmain"
  #query: []
  #fragment: null
  #pageName: "page"
}

我该如何修复 ?page=2 错误?


感谢回复

我只想使用分页,但是当我点击第 2 页时我的代码显示错误

这是我不使用引导组件的起源

web.php

Route::get('rows','ProController@rows');

ProController:

public function rows(Request $request)
{
    $search_cons=$request->all();

$tablename=$search_cons['tablename'];

$search_alls = DB::table($tablename)
    ->where('product','=','5')
    ->paginate(9);

    return View('pro.results')
            ->with('search_alls', $search_alls)
            ->with('table',$tablename);

};

results.blade 视图

<div class="container">
@foreach ($search_alls->chunk(3) as $chunks)
    <div class="row clearfix">
      @foreach($chunks as $searchall)
        <div class="col-md-4 column">
                <h3>
                    label
                </h3>
                <p>
                    something             
        </p>
        </div>
      @endforeach
    </div>
@endforeach
</div>
{!! $search_alls->links() !!}

第一页效果很好

http://localhost:8000/rows?table=A16&...

当我点击第二页时

localhost:8000/rows?page=2

它显示

它显示未定义的索引:表名

【问题讨论】:

  • 问题是什么?你有一个错误 Undefined index: tablename OR ?page=2 error? ?除了通常的分页案例之外,您能否指定您的需求更改?如果您遵循官方的 Laravel Pagination 格式,则无需担心其他任何事情。
  • @robspin 不清楚你的问题请描述更多,然后我们会帮助你。
  • @EazySam 我为 ?page=2 重新发布我的问题
  • @AddWebSolutionPvtLtd 我重新发布我的问题只是使用 laravel 分页功能
  • 使用原始分页我用这个来修复。 {{ $users->appends($_GET)->links() }} 但是我可以使用 post 方法进行分页 route::post('rows','ProController@rows') 和 {{ $users->appends( $_POST)->links() }} 我收到错误消息 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

标签: laravel pagination bootstrap-4 custom-component


【解决方案1】:

是的,因为您需要在分页链接中附加当前查询字符串,这是我的代码

$input = Request::input();
$myModelsPaginator = App\Models\MyModel::paginate();
$myModelsPaginator->appends($input);

为你喜欢这个

 $input = Request::input();
 $search_alls = DB::table($tablename)
 ->where(‘product’,’=’,’5');
->pagination(9);
 $searcg_alls->appends($input);

用页面链接附加你的表名和所有其他输入,这样你就不会得到 table not found 错误

你的问题不清楚,但假设你想要的不是,那么评论可能对你有帮助

【讨论】:

  • 感谢您的回复。我可以使用查询生成器从数据库中获取数据吗?不使用 Eloquent ORM,因为表会改变每个查询,而模型也会改变。
  • 但是当我使用查询生成器时,我必须从请求中获取变量。但是不能向?page=2发送请求,因为page2是分页功能,那么$tablename=$search_cons['tablename']会报错
  • 因为您的表名来自请求权。并且您的分页链接不包含查询字符串,首先您在分页中附加查询字符串,如上 ans 我告诉您如何将查询字符串附加到分页链接中
  • 感谢您的帮助。我可以在分页功能上使用 post 方法吗?当我去错误消息: Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message
  • 我发现答案分页只适用于获取参数。
猜你喜欢
  • 2021-07-13
  • 1970-01-01
  • 2016-09-21
  • 2015-06-09
  • 2017-02-28
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多