Подтвердить что ты не робот

Как использовать разбиение на страницы в laravel 5?

Я пытаюсь перенести приложение laravel4 на laravel 5. В предыдущей версии я мог использовать следующий метод для генерации URL-адресов для разбивки на страницы.

В контроллере:

$this->data['pages']= Page::whereIn('area_id', $suburbs)->where('score','>','0')->orderBy('score','desc')->paginate(12);

и после совместного использования массива данных с представлением я мог бы

В представлениях:

{{$pages->links()}}

В laravel 5 это приводит к ошибке

ErrorException in AbstractPaginator.php line 445:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'links'

Не уверен, что я здесь пропал, может кто-нибудь помочь?

4b9b3361

Ответ 1

В Laravel 5 нет метода "ссылки", вы можете попробовать это

{!! $pages->render() !!}

Ответ 2

В других рамках, разбиение на страницы может быть очень болезненным. Laravel 5 делает его легким. Для его использования сначала необходимо внести изменения в свой код контроллера, где вы вызываете данные из базы данных:

 public function index()
{       
        $users = DB::table('books')->simplePaginate(5);
        //using pagination method
        return view('index', ['users' => $users]);
}

... после этого вы можете использовать этот код:

<?php echo $users->render(); ?>

Это заставит вас использовать простую красоту Laravel 5.

Ответ 4

$items = SomeDataModel->get()->paginate(2); // in your controller

@foreach(items as $item)   // in your view.blade file
....echo some data here
@endforeach

<div class="pagination">
    {{ $items->render() }} or {{ $items->links() }}
</div>

Используйте имя массива (items) в методе render() или links(), а НЕ в элементе массива. Это сработало для меня.

Ответ 5

@if ($posts->lastPage() > 1)
        <nav aria-label="Page navigation">
            <ul class="pagination">
                @if($posts->currentPage() != 1 && $posts->lastPage() >= 5)
                <li>
                    <a href="{{ $posts->url($posts->url(1)) }}" aria-label="Previous">
                        <span aria-hidden="true">First</span>
                    </a>
                </li>
                @endif
                @if($posts->currentPage() != 1)
                <li>
                    <a href="{{ $posts->url($posts->currentPage()-1) }}" aria-label="Previous">
                        <span aria-hidden="true">&#x3C;</span>
                    </a>
                </li>
                @endif
                @for($i = max($posts->currentPage()-2, 1); $i <= min(max($posts->currentPage()-2, 1)+4,$posts->lastPage()); $i++)
                @if($posts->currentPage() == $i)
                <li class="active">
                @else
                <li>
                @endif
                    <a href="{{ $posts->url($i) }}">{{ $i }}</a>
                </li>
                @endfor
                @if ($posts->currentPage() != $posts->lastPage())
                <li>
                    <a href="{{ $posts->url($posts->currentPage()+1) }}" aria-label="Next">
                        <span aria-hidden="true">&#x3E;</span>
                    </a>
                </li>
                @endif
                @if ($posts->currentPage() != $posts->lastPage() && $posts->lastPage() >= 5)
                <li>
                    <a href="{{ $posts->url($posts->lastPage()) }}" aria-label="Next">
                        <span aria-hidden="true">Last</span>
                    </a>
                </li>
                @endif
            </ul>
        </nav>
        @endif

Ответ 6

Привет, мой код для разбивки на страницы: Использовать в поле зрения:
@include ('pagination.default', ['paginator' = > $users])

Views/пагинация/default.blade.php

@if ($paginator->lastPage() > 1)
    <ul class="pagination">
        <!-- si la pagina actual es distinto a 1 y hay mas de 5 hojas muestro el boton de 1era hoja -->
        <!-- if actual page is not equals 1, and there is more than 5 pages then I show first page button -->
        @if ($paginator->currentPage() != 1 && $paginator->lastPage() >= 5)
            <li>
                <a href="{{ $paginator->url($paginator->url(1)) }}" >
                    <<
                </a>
            </li>
        @endif

        <!-- si la pagina actual es distinto a 1 muestra el boton de atras -->
        @if($paginator->currentPage() != 1)
            <li>
                <a href="{{ $paginator->url($paginator->currentPage()-1) }}" >
                    <
                </a>
            </li>
        @endif

        <!-- dibuja las hojas... Tomando un rango de 5 hojas, siempre que puede muestra 2 hojas hacia atras y 2 hacia adelante -->
        <!-- I draw the pages... I show 2 pages back and 2 pages forward -->
        @for($i = max($paginator->currentPage()-2, 1); $i <= min(max($paginator->currentPage()-2, 1)+4,$paginator->lastPage()); $i++)
                <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
                    <a href="{{ $paginator->url($i) }}">{{ $i }}</a>
                </li>
        @endfor

        <!-- si la pagina actual es distinto a la ultima muestra el boton de adelante -->
        <!-- if actual page is not equal last page then I show the forward button-->
        @if ($paginator->currentPage() != $paginator->lastPage())
            <li>
                <a href="{{ $paginator->url($paginator->currentPage()+1) }}" >
                    >
                </a>
            </li>
        @endif

        <!-- si la pagina actual es distinto a la ultima y hay mas de 5 hojas muestra el boton de ultima hoja -->
        <!-- if actual page is not equal last page, and there is more than 5 pages then I show last page button -->
        @if ($paginator->currentPage() != $paginator->lastPage() && $paginator->lastPage() >= 5)
            <li>
                <a href="{{ $paginator->url($paginator->lastPage()) }}" >
                    >>
                </a>
            </li>
        @endif
    </ul>
@endif