【问题标题】:Pagination and JOIN with Laravel使用 Laravel 进行分页和 JOIN
【发布时间】:2016-09-25 09:34:49
【问题描述】:

在使用 Eloquent ORM 进行 JOIN 后尝试对某些结果进行分页时,我遇到了一种奇怪的行为(我猜是我的错)。

这是导致 Laravel 返回空白(未显示错误)500 错误页面的代码:

return Entry::join('articles', 'entries.id', '=', 'articles.entryID')
        ->orderBy('articles.created_at', 'desc')
        ->paginate(15, array('articles.*'));

如果我将paginate(...)get(...) 交换,页面将正确显示并返回数据。

编辑

这就是我创建articlesentries 表的方式:

// 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');
});

编辑 2

这是我在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} [] []

编辑 3

我已经禁用了内存限制和日志

DB::connection()->disableQueryLog();
ini_set('memory_limit', '-1');

但整个系统会冻结大约 1 分钟,就像它开始循环一样。

编辑 4

我也在这里添加了我的问题:http://help.laravel.io/00f4a0793d2291d73214e0b6f56320b84caaf51d

编辑 5

我的系统似乎有问题,或者是 Laravel 中的错误。

我也尝试过手动创建分页器:

$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();

页面仍未呈现,我不断收到错误:Allowed memory size of 134217728 bytes exhausted

【问题讨论】:

  • 你是怎么解决这个问题的?

标签: laravel pagination


【解决方案1】:

查看 Eloquent 的分页方法;从理论上讲,它应该工作。 Eloquent 很棒,但就个人而言,我发现 Fluent 更适合使用 join()s 进行查询

return DB::table('entries')
        ->join('articles', 'entries.id', '=', 'articles.entryID')
        ->orderBy('articles.created_at', 'desc')
        ->select('articles.*')
        ->paginate(15);

【讨论】:

  • 这正是我尝试过的,但我仍然得到一个空白页,500 错误状态:S
  • 看到get() 适合你,我认为是articles.*。当您将通配符仅用于调试目的时会发生什么?选择('articles.entryID')。或者,将select() 一起删除。
  • 如果我删除通配符,我会得到错误页面,但每当我尝试使用 select('articles.entryID') 时,我仍然会得到完全没有错误的空白页面。现在,这可能是数据库问题吗?
  • 有可能。你能张贴你的两张桌子SHOW CREATE TABLE吗?
  • 我唯一能想到的是:由于分页器使用 id 字段进行聚合,因此您的 $table->bigIncrements('id'); 可能会导致问题。这是一种受支持的方法,但是,我在使用传统的 $table->increments('id'); 时从未遇到任何问题。此外,使用 Laravel 命名约定,ectryID 应该是 entry_id。这不应该有所作为,您构建查询的方式,而只是一个想法。你在app/storage/logs/laravel.log有什么与他有关的吗?
【解决方案2】:

我刚刚发现问题是由以下原因引起的:

dd($pagination)

看起来我无法转储函数返回的分页器对象。

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 2020-03-18
    • 2014-09-01
    • 2019-03-12
    • 2019-11-01
    • 1970-01-01
    • 2015-01-01
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多