LoginSignup
0
0

More than 3 years have passed since last update.

[Laravel] CRUD 詳細画面

Posted at

概要

よくあるCRUDの個人メモ。
一覧画面から詳細画面への遷移を簡単にまとめる。

前提条件

以下記事を参考に一覧画面を作成していること。
https://qiita.com/Hiroto10/items/4723b96b491b1ce88278

コントローラ

ルーティングを確認すると、詳細画面はTasksController@showを使用することがわかるので編集。

app/Http/Controllers/TasksController.php
    public function show($id)
    {
        $task = Task::findOrFail($id);

        return view('tasks.show', [
            'task' =>  $task
        ]);
    }

view

詳細画面

一覧画面同様、テーブル形式で出力させる。

resources/views/tasks/show.blade.php
@extends('layouts.app')
@section('content')

  <table class="table table-striped">
    <tr>
      <th>id</th>
      <td>{{ $task->id }}</td>
    </tr>
    <tr>
      <th>タスク</th>
      <td>{{ $task->task }}</td>
  </tr>
  </table>

@endsection

一覧画面

一覧画面のidが押下されたら詳細画面に飛ぶよう修正する。

resources/views/tasks/index.blade.php
@extends('layouts.app')
@section('content')
  @if (count($tasks) > 0)
    <table class="table table-striped">
      <thead>
        <tr>
          <th>id</th>
          <th>タスク</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($tasks as $task)
        <tr>
          <td>{!! link_to_route('tasks.show', $task->id, ['task' => $task->id], []) !!}</td>
          <td>{{ $task->task }}</td>
        </tr>
        @endforeach
      </tbody>
    </table>
  @endif
@endsection

スクリーンショット 2020-09-30 23.43.38.png

idを押下し、詳細画面へ遷移すればok。


以上

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