1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel ペジネート memo*

Last updated at Posted at 2019-04-19

このページの下のようなページネーションという機能です。
ここでは1つのページに5つだけ表示して、それ以降は次のページに・・・

スクリーンショット (2).png

編集するのは、2つのファイル

1つ目↓

コントローラーに paginate(表示したい数) を指定する。

ItemController.php
    public function index()
    {
        $items = Item::paginate(5);  // ()の数字は表示したい要素の数
        return view('items.index', compact('items'));
    }

2つ目↓

index.blade.php
@extends('layouts.app')
@section('title', '商品一覧')
@section('content')
 {{ link_to_route('items.create','新規登録',[],['class'=> 'btn btn-primary'])  }}
<table class="table table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>商品名</th>
        </tr>
    </thead>
    <tbody>
    @foreach($items as $item)
        <tr>
            <td>{{  link_to_route('items.show',$item->id,['item' => $item->id]) }}</td>
            <td>{{$item->name}}</td>
            <td>{{ link_to_route('items.edit', '編集', ['id' => $item->id], ['class' => 'btn btn-default']) }}</td>
            <td>
                {{ Form::open(['route' => ['items.destroy', $item->id], 'method' => 'delete']) }}
                {{ Form::submit('削除', ['class' => 'btn btn-danger']) }}
                {{ Form::close() }}
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
 {{ $items->links() }}  //この一行を追加★!
@endsection

完成!

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?