【问题标题】:How to show on view some of the articles depending on their order (Laravel 8)如何根据文章的顺序在视图中显示某些文章 (Laravel 8)
【发布时间】:2022-12-19 17:25:00
【问题描述】:

我想在不使用分页方法的情况下对我的 mysql 数据库进行分页,特别是我想根据他们的 publication_date row_num 显示文章,每页只显示 8 篇文章,我还想直接在控制器中添加 row_num,因为我'我已经在我的数据库中添加了一列,但我想删除它。 desc_id 是我要删除的列。

我尝试像这样修改控制器:

`

public function pages($id = null){
        $articles_show = Article::where(function ($query) {
            $query->where(function ($query2) {
                $query2->where('draft', 0)->whereNull('publication_date');
            })->orWhere(function ($query2) {
                $query2->where('draft', 0)->where('publication_date', '<=', DateHelper::currentDateTime());
            });
        })->orderBy('publicated_at', 'DESC');
        (int)$this_page = \Request::segment(2);
        if ($this_page == null) {
            $this_page = 1;
        }
        $articles = array();
        $max_desc_id = $this_page * 8;
        $min_desc_id = $max_desc_id - 7;
        foreach ($articles_show as $article) {
            if ($article->desc_id >= $min_desc_id && $article->desc_id <= $max_desc_id) {
                $articles[] = $article;
            }
        }

        return view('allArticles.page'.$this_page)->with('articles', $articles)->with('this_page', $this_page);
    }

` 但它告诉我 $articles 是空的。 作为参考,这是我的观点:

`

@foreach($articles as $article)
                                    <div class="entry col-sm-6 col-12">
                                        <div class="grid-inner thumbnail">
                                            <div class="entry-image">
                                                <a href="{{url($article->permalink)}}">
                                                    <picture>
                                                        <source srcset="{{url($article->image('large','webp'))}}" type="image/webp">
                                                        <source srcset="{{url($article->image('large','jpg'))}}" type="image/jpeg">
                                                        <img loading="lazy" data-src="" alt="{{$article->image_alt}}" width="623" height="348.88">
                                                    </picture>
                                                </a>
                                            </div>
                                            <div class="entry-title">
                                                <h2><a href="{{url($article->permalink)}}">{{$article->h1}}</a></h2>
                                            </div>
                                            <div class="entry-meta">
                                                <ul>
                                                    <li><i class="icon-calendar3"></i>{{$article->data_pubblicazione}}</li>
                                                    <li><a href="{{url($article->author_permalink)}}"><i class="icon-user"></i>{{$article->autore}}</a></li>
                                                    <li><a href="{{url($article->category->permalink)}}"><i class="icon-folder-open"></i> {{$article->category->name}}</a></li>
                                                </ul>
                                            </div>
                                            <div class="entry-content">
                                                <p>{{substr(StringHelper::plainText($article->context), 0, 250) . '...'}}</p>
                                                <a href="{{url($article->permalink)}}" class="more-link custom-color">Leggi</a>
                                            </div>
                                        </div>
                                    </div>
                                    @endforeach

`

【问题讨论】:

    标签: mysql pagination laravel-8 laravel-controller


    【解决方案1】:

    您忘记在模型请求后添加 ->get(count)

    $articles_show = Article::where(function ($query) {
            $query->where(function ($query2) {
                $query2->where('draft', 0)->whereNull('publication_date');
            })->orWhere(function ($query2) {
                $query2->where('draft', 0)->where('publication_date', '<=', DateHelper::currentDateTime());
            });
        })->orderBy('publicated_at', 'DESC')->get(8);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-06
      • 2019-03-13
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多