使用 Laravel 进行分页和 JOIN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25509196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pagination and JOIN with Laravel
提问by siannone
I'm having a strange behavior (due to my fault I guess) while trying to paginate some results after a JOIN using Eloquent ORM.
在尝试使用 Eloquent ORM 进行 JOIN 后对某些结果进行分页时,我有一个奇怪的行为(我猜是我的错)。
This is the code that causes Laravel to return a blank (no errors are shown) 500 error page:
这是导致 Laravel 返回空白(未显示错误)500 错误页面的代码:
return Entry::join('articles', 'entries.id', '=', 'articles.entryID')
->orderBy('articles.created_at', 'desc')
->paginate(15, array('articles.*'));
If I swap paginate(...)
with get(...)
the page is correctly shown and data is returned.
如果我paginate(...)
与get(...)
页面交换正确显示并返回数据。
Edit:
编辑:
This is how I create both the articles
and entries
tables:
这就是我创建articles
和entries
表的方式:
// Entries
Schema::create('entries', function($table) {
$table->string('app', 20);
$table->bigInteger('comments');
$table->bigIncrements('id');
$table->string('owner', 255)
->foreign('owner')
->references('username')
->on('users');
$table->dateTime('timestamp');
$table->bigInteger('views');
$table->bigInteger('votes');
$table->dateTime('created_at');
$table->dateTime('deleted_at');
$table->dateTime('updated_at');
});
// Articles
Schema::create('articles', function($table) {
$table->bigIncrements('id');
$table->string('author')
->foreign('author')
->references('username')
->on('users');
$table->text('content');
$table->bigInteger('entryID')
->foreign('entryID')
->references('id')
->on('entries');
$table->string('status', 20);
$table->string('summary', 1000);
$table->string('title', 255);
$table->dateTime('created_at');
$table->dateTime('deleted_at');
$table->dateTime('updated_at');
});
Edit 2:
编辑2:
This is what I get in laravel.log
:
这是我得到的laravel.log
:
[2014-08-26 19:59:54] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Allowed memory size of 134217728 bytes exhausted (tried to allocate 129499136 bytes)' in /usr/share/nginx/html/webname/vendor/laravel/framework/src/Illuminate/Support/helpers.php:605
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
Edit 3:
编辑3:
I've disabled the memory limit and the log with
我已经禁用了内存限制和日志
DB::connection()->disableQueryLog();
ini_set('memory_limit', '-1');
but the whole system freezes for about 1 minute like it starts a loop or something.
但是整个系统冻结了大约 1 分钟,就像它启动了一个循环或其他什么一样。
Edit 4:
编辑4:
I've added my issue here too: http://help.laravel.io/00f4a0793d2291d73214e0b6f56320b84caaf51d
我也在这里添加了我的问题:http: //help.laravel.io/00f4a0793d2291d73214e0b6f56320b84caaf51d
Edit 5:
编辑 5:
It looks like there's something wrong with my system or it's a bug in Laravel.
看起来我的系统有问题或者是 Laravel 中的错误。
I have also tried to manually create the paginator:
我还尝试手动创建分页器:
$entries = Entry::join('articles', 'entries.id', '=', 'articles.entryID')
->orderBy('entries.created_at', 'desc')
->get(); // Data is correctly retrieved from the DB
$paginator = Paginator::make($entries->toArray(), self::count());
dd($paginator);
die();
The page is still not rendered and I keep receiving the error: Allowed memory size of 134217728 bytes exhausted
该页面仍未呈现,我不断收到错误消息: Allowed memory size of 134217728 bytes exhausted
采纳答案by siannone
I just found out that the problem was caused by:
我刚刚发现问题是由以下原因引起的:
dd($pagination)
dd($pagination)
Looks like I cannot dump the paginator object returned by the function.
看起来我无法转储函数返回的分页器对象。
回答by c-griffin
Looking at the paginate method for Eloquent; theoretically, it should work. Eloquent is fantastic, but personally, I've found that Fluent is better for queries with join()
s
查看 Eloquent 的分页方法;从理论上讲,它应该可以工作。Eloquent 很棒,但就我个人而言,我发现 Fluent 更适合使用join()
s进行查询
return DB::table('entries')
->join('articles', 'entries.id', '=', 'articles.entryID')
->orderBy('articles.created_at', 'desc')
->select('articles.*')
->paginate(15);