简体   繁体   English

Laravel 5.7 分页

[英]Laravel 5.7 Pagination

I am working with an existing database where I have a table named pnc_members我正在使用一个现有的数据库,其中有一个名为pnc_members的表

Because laravel interprets that name incorrectly I placed因为 laravel 错误地解释了我放置的名称

protected $table = 'pnc_members';

in the pnc_members.php model to solve that problem.pnc_members.php模型中解决该问题。

In my controller I get the table data with在我的控制器中,我得到了表数据

$members = pnc_members::paginate(10);

however;然而; I get an error in the view members.blade.php when I insert the pagination link插入分页链接时,在视图members.blade.php出现错误

{{ $members->links() }}

The error is ErrorException (E_ERROR) Call to undefined method App\\Models\\pnc_members::links() (View: C:\\wamp64\\www\\pnc-system\\resources\\views\\members.blade.php) Previous exceptions Call to undefined method App\\Models\\pnc_members::links()错误是 ErrorException (E_ERROR) Call to undefined method App\\Models\\pnc_members::links() (View: C:\\wamp64\\www\\pnc-system\\resources\\views\\members.blade.php) Previous exceptions Call to undefined方法 App\\Models\\pnc_members::links()

So it seems like it is confusing $members with the protect directive for the table!因此,它似乎将$members与表的保护指令混淆了!

I've tried changing the variable to another name used in the get (ie $members_pg) and it still does not work!我已经尝试将变量更改为 get 中使用的另一个名称(即 $members_pg),但它仍然不起作用!

Any help will be much appreciated.任何帮助都感激不尽。

I figured it out!我想到了!

I added a line to the controller $pages = $members->links();我在控制器中添加了一行$pages = $members->links(); then used dd($pages);然后使用dd($pages); to see what it captured and that looked good.看看它捕获了什么,看起来不错。

The returned view adding array with the $pages variable to be passed to the blade as follows: return view('members', ['pages' => $pages])->with('members', $members);返回的视图添加数组,带有要传递给刀片的 $pages 变量,如下所示: return view('members', ['pages' => $pages])->with('members', $members);

I then put {{$pages} into my blade and it works perfectly.然后我将 {{$pages} 放入我的刀片中,它运行良好。

Why it does not work the other way I don't really know, but at least I got it resolved.为什么它不能以另一种方式工作,我真的不知道,但至少我解决了。

Thanks//谢谢//

If your table is pnc_members, your Model name must be PncMember.如果您的表是 pnc_members,则您的模型名称必须是 PncMember。

So first of all make sure you have this model and then in your controller you can use所以首先确保你有这个模型,然后在你的控制器中你可以使用

$members = PncMember::paginate(5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM