【问题标题】:Is this possible to paginate laravel with condition?这可以用条件对 laravel 进行分页吗?
【发布时间】:2019-12-02 19:06:56
【问题描述】:

我想在单个帖子页面中进行分页,数据来自“内容”表。但是我想在捕获特定单词(部分)作为标题时进行分页,然后在同一篇文章中转到下一页。 这可以用 Laravel 5.4 实现吗?

示例:

第 1 节(这是标题)

文字文字文字文字文字文字文字

(下一页>我想添加下一页,它将进入第2页)

第 2 节

文本文本文本文本文本文本文本 (下一页>)

我的 Post.php

 public function contents()
{
    return $this->hasMany(Content::class);
}

刀片视图:

@if($post->contents)
          @foreach ($post->contents as $content)
                    @if ($content->type == "header")
                    <h4>{{ $content->body }}</h4>
                    @endif

                    @if ($content->type == "text")
                    <p>{{ $content->body }}</p>
                    @endif
          @endforeach
@endif

【问题讨论】:

  • 所以基本上,您想要一个基于模型数据的可变页面大小?我认为这是不可能的,分页是在查询时完成的,所以稍后更改值会弄乱分页数据。
  • 我想为单个帖子添加部分,因为单个页面的文本长度太长了。

标签: php laravel pagination


【解决方案1】:

在模型中使用paginate() 进行分页。

public function contents()
{
    return $this->hasMany(Content::class)->paginate(1);
}

查看文件

@if($post->contents)
    @foreach ($post->contents() as $content)
        @if ($content->type == "header")
            <h4>{{ $content->body }}</h4>
        @endif
        @if ($content->type == "text")
            <p>{{ $content->body }}</p>
        @endif
    @endforeach
    {{ $post->contents()->links() }}
@endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多