Search code examples
mysqlpaginationlaravel-8laravel-controller

How to show on view some of the articles depending on their order (Laravel 8)


I want to paginate my mysql database without using the paginate method, in particular i want to show the articles depending on their publication_date row_num, only showing 8 articles per page, i would also like to add the row_num directly in the controller, because i've added a column in my database with it, but i would like to remove it. desc_id is the column i would like to remove.

I tried modifying the controller like this:

`

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

` but it gives me that $articles is empty. for reference this is my view:

`

@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

`


Solution

  • You are forget add ->get(count) after model request

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