PHPフレームワークLaravel5.6でCRUD~READ編

一覧ページindexと個別ページshowを作る。

index:一覧ページ

【コントローラー修正】

TestController.phpのindexメソッドを修正するよ。

app/Http/Controllers/TestController.php

    public function index()
    {
        $data = [];
        $data['tests'] = Test::all();
        $data['categories'] = self::CATEGORY_LIST;
        return view('test/index', $data);
    }

※Modelのall()はstaticメソッドだからインスタンス生成しなくてもいい。

【ビュー】

resources/views/test/index.blade.php

@extends('layouts.app')

@section('content')
<div class="container">

  <div class="panel panel-default">
    <h2 class="text-white bg-secondary">一覧</h2>
    <a href="/test/create"><button type="button" class="btn btn-primary">新規追加</button></a>

    <table class="table">
      <tr>
        <th>ID</th>
        <th>タイトル</th>
        <th>カテゴリ</th>
      </tr>
      @foreach ($tests as $test)
      <tr>
        <td>{{ $test->id }}</td>
        <td><a href="/test/{{ $test->id }}">{{ $test->title }}</a></td>
        <td>{{ $categories[$test->category] }}</td>
        <td>
      </tr>
      @endforeach
    </table>

  </div>

</div>
@endsection

※あらかじめ「ID」にEDITリンク、「タイトル」にSHOWのリンクを設置。

show:個別ページ

【コントローラー修正】

TestController.phpにshowメソッドを追加する。

app/Http/Controllers/TestController.php

    public function show($id)
    {
        $data = [];
        $data['categories'] = self::CATEGORY_LIST;
        $data['test'] = Test::find($id);
        return view('test/show', $data);
    }

★Eloquentのfindメソッドは主キーを指定して検索する!

【ビュー】

resources/views/test/show.blade.php

@extends('layouts.app')

@section('content')
<div class="container">

  <div class="panel panel-default">
    <h2 class="text-white bg-secondary">表示</h2>
    <a href="/test"><button class="btn btn-primary">一覧に戻る</button></a>

    @if (!$test)
    <p>Data is Nothing!</p>
    @else

    <div class="form-group">
      <h3 class="text-white bg-dark">ID</h3>
      <p>{{ $test->id }}</p>
    </div>
    <div class="form-group">
      <h3 class="text-white bg-dark">タイトル</h3>
      <p>{{ $test->title }}</p>
    </div>
    <div class="form-group">
      <h3 class="text-white bg-dark">カテゴリ</h3>
      <p>{{ $categories[$test->category] }}</p>
    </div>
    <div class="form-group">
      <h3 class="text-white bg-dark">説明</h3>
      <p>{!! nl2br(e($test->description)) !!}</p>
      <p>{{ $test->description }}</p>
      <p>{ nl2br($test->description) }</p>
      <p>{ nl2br(e($test->description)) }</p>
      <p>{!! nl2br(e($test->description)) !!}</p>
    </div>

    <div class="d-inline-block">
      <a href="/test/{{ $test->id }}/edit"><button class="btn btn-success">変更</button></a>
    </div>
    <div class="d-inline-block">
      <form action="{{ $test->id }}" method="POST" onsubmit="return confirm('削除してもよろしいですか?');">
        @method('DELETE')
        <button type="submit" class="btn btn-danger">削除</button>
      </form>
    </div>

    @endif

  </div>
</div>
@endsection

★bladeで改行(複数行の表示)するには

①e()ヘルパーでHTMLエスケープ
e($test->description)
してから

②nl2brで改行コードを
に変換
nl2br(e($test->description))
した後で

③{{!! !!}}でタグ出力ね!
{!! nl2br(e($test->description)) !!}

※Laravel5.6から @method(‘DELETE’) で

を生成してくれるよ。