LoginSignup
0
0

More than 5 years have passed since last update.

laravel paginate で ページネーション

Last updated at Posted at 2018-12-24

コントローラー

paginate(2) で 1ページに 2件ずつ表示するってこと。


<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Route;
use Log;

//使いたいモデルを指定
use App\Nikki;


class NikkiController extends Controller
{
    public function show()
    {

        $nikkis = Nikki::query()
            ->paginate(2);

        return view('nikki.show', ['nikkis' => $nikkis]);
    }

}

表示

ページ数に応じて、
ページャーもこれでいい感じに動く。


@foreach ($nikkis as $v)
    <div class="card mb-2">
        <div class="card-body">
            <h4 class="card-title">{{ $v->title }}</h4>
            <h6 class="card-subtitle mb-2 text-muted">{{ $v->updated_at }}</h6>
            <p class="card-text">{{ $v->body }}</p>
            <a href="/nikki/add/{{ $v->id }}" class="card-link">修正</a>
            <a href="/nikki/del/{{ $v->id }}" class="card-link">削除</a>
        </div>
    </div>
@endforeach





    @if($nikkis->lastPage() > 1)

        @if($nikkis->previousPageUrl())
            <a href="{{ $nikkis->previousPageUrl() }}">前のページへ</a>
        @endif

        {{ $nikkis->currentPage() }} / {{ $nikkis->lastPage() }}

        @if($nikkis->nextPageUrl())
            <a href="{{ $nikkis->nextPageUrl() }}">次のページへ</a>
        @endif

    @endif

以上。
めちゃくちゃ簡単ですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0