Почему Laravel нумерация страниц показывает ту же страницу? - PullRequest
0 голосов
/ 30 января 2020

У меня есть модуль блога в моем laravel с несколькими категориями. В других категориях нумерация страниц работает просто отлично, но при использовании метода этой категории ссылки () всегда показывают первую страницу. Это мой код:

  $page = Categoriepage::where('identifiant', 'orientation')->first();

    $idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');

    $articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);

    $links = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);

    return View::make('pages.orientation')->with('articles',$articles)->with('links',$links);

и на мой взгляд:

  <?php $i = 1 ?>

                <div class="masonry ">

                @foreach($articles as $article)

                    @include('components.article-or')

                <?php $i++ ?>

                @endforeach()

                </div>


                <div class="text-center" style="margin-top: 5%">

                    {{ $links->links() }}

                </div>

Ответы [ 3 ]

2 голосов
/ 30 января 2020

Вы всегда выбираете первые 6 статей, потому что отображаемые статьи поступают из:

$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);

, а не из запроса разбивки на страницы.

Нет необходимости отделять запрос для ссылок и запрос для фактических статей. Вместо:

$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
$links = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);

Вы можете сделать:

$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);

и по вашему мнению:

<div class="masonry ">
   @foreach($articles as $article)
     @include('components.article-or')
    @endforeach
</div>

<div class="text-center" style="margin-top: 5%">
  {{ $articles->links() }}
</div>             
2 голосов
/ 30 января 2020

Я думаю, вы немного перепутали это.

Вы получаете все статьи (->get()) и берете первые шесть (->take(6)). А затем вы получаете $links, используя ->paginator(6).

Это можно и нужно делать, используя только ->paginator(6), таким образом Laravel извлекает правильные 6 статьи для ?page=X, на которых вы находитесь, и отображает ссылки на основе общего количества статьи найдены по запросу.

Попробуйте это так:

$page = Categoriepage::where('identifiant', 'orientation')->first();

$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');

$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);

return View::make('pages.orientation')->with('articles',$articles)
                <?php $i = 1 ?>

                <div class="masonry ">
                    @foreach($articles as $article)
                        @include('components.article-or')

                        <?php $i++ ?>
                    @endforeach
                </div>

                <div class="text-center" style="margin-top: 5%">
                    {{ $articles->links() }}
                </div>
1 голос
/ 30 января 2020

вам не нужно делать два запроса, а только один

 $page = Categoriepage::where('identifiant', 'orientation')->first();

$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');

$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);


return View::make('pages.orientation')->with('articles',$articles);

На ваш взгляд.

             <div class="masonry ">

            @foreach($articles as $article)

                @include('components.article-or')

            <?php $i++ ?>

            @endforeach()

            </div>


            <div class="text-center" style="margin-top: 5%">

                {{ $articles->links() }}

            </div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...