Laravel | BBS | Show の実装

ホーム  

概要

前回の BBS は操作が分かりずらかったので作り直しました。デザインも変わりました。

WEB アプリケーションには、CRUD という機能をつけると良いとされています。

CRUD とは、Create (作成)、Read (読み込み)、Update (更新)、Delete (削除)の頭文字をとったものです。

Laravel には、Read (読み込み) として、Index と Show の2つが用意されています。Index で一覧を表示して、Show で詳細を表示するという具合に使われます。


インデックスページの変更

resources / views / index.blade.php を次のように書きかえてください。

<!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BBS</title>
    <link rel="stylesheet" href="{{ url('style.css')}}">
    </head>
    <body>
    <div class="container">
    <h1 class="margin-zero">BBS</h1>
    <ul>
    @forelse ($posts as $post)
    <li class="h-layout">   {{-- ここを変更 --}}
        <div class="post-sub">
            <p class="v-center">{{Str::limit($post->body, 80)}}</p>
        </div>
        {{-- ここから --}}
        <div>
            <form method="GET" action="{{route('posts.show', $post)}}">
            @csrf
            <button class="btn-absolute">Show</button>
            </form>
        </div>
        {{-- ここまで追加 --}}
    </li>
    @empty
    <li class="no-title">No post!</li>
    @endforelse
    </ul>
    </div>
    </body>
    </html>

php artisan serve で BBS アプリを起動して、localhost:8000 で確認してください。


CSS ファイルの変更

style.css を次のように変更してください。

body {
    background-color: #efefef;
    color: gray;
    font-family: sans-serif;
}

.container {
    width: 400px;
    margin: 32px auto;
}

h1 {
    font-size: 20px;
    color: gray;
}

ul {
    padding-left: 0;
}

li {
    list-style: none;
    margin-top: 16px;
}

.post-sub {
    width: 100%;
    height: 80px;
    background-color: white;
    border-radius: 4px;
    border: solid 1px lightgray;
}

.v-center {
    margin: 8px;
}

.no-title {
    font-size: small;
    text-align: center;
    color: gray;
}

/* ここから */
.h-layout {
    display: flex;
    position: relative;
}

.btn-absolute {
    position: absolute;
    right: 4px;
    bottom: 4px;
}

button {
    color: gray;
}
/* ここまで追加 */

php artisan serve で BBS アプリを起動して、localhost:8000 で確認してください。


PostController の変更

App / Http / Controlles の PostController.php を次のように変更します。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->get();

        return view('index')->with(['posts' => $posts]);
    }

    // ここから
    public function show(Post $post)
    {
        return view('posts.show')->with(['post'=>$post]);
    }
    // ここまで追加
}


リレーションメソッドの追加

Post.php と Comment.php の二つのモデルにリレーションのためのメソッドを追加します。

app / Models / Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'body',
    ];

    // ここから
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
    // ここまで追加
}

return $this->hasMany(Comment::class); の hasMany が Post からは Comment を多数持てることを表しています。

app / Models / Comment.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = [
        'body',
        'post_id',
    ];

    // ここから
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
    // ここまで追加
}

return $this->belongsTo(Post::class); の belongsTo が Comment からは Post を一つしか持てないことを表しています。


Show ページの作成

resources / views の中に posts というフォルダを作り、その中に show.blade.php というファイルを作ってください。 そして show.blade.php を次のようにコーディングします。

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Show | BBS</title>
    <link rel="stylesheet" href="{{url('style.css')}}">
</head>
<body>
<div class="container">
<div>
<h1 class="margin-zero">Show</h1>
</div>
    <ul>
    <li>
        <div class="post-all">
            <p class="margin-eight">{{$post->body}}</p>
        </div>
    </li>
    </ul>

    <p class="small-title">comment</p>

    <ul>
    @forelse ($post->comments as $comment)
    <li>
    <div class="comment">{{ $comment->body }}</div>
    </li>
    @empty
    <li class="no-title">No comment.</li>
    @endforelse
    </ul>
    <p class="back-link"><a href="{{route('posts.index')}}">Back to Index</a></p>
    </div>
</body>
</html>

php artisan serve で BBS アプリを起動して、localhost:8000 で確認してください。


CSS ファイルの変更

再び style.css に変更を加えます。public / style.css に次のような追加をしてください。

body {
    background-color: #efefef;
    color: gray;
    font-family: sans-serif;
}

.container {
    width: 400px;
    margin: 32px auto;
}

h1 {
    font-size: 20px;
    color: gray;
}

ul {
    padding-left: 0;
}

li {
    list-style: none;
    margin-top: 16px;
}

.post-sub {
    width: 100%;
    height: 80px;
    background-color: white;
    border-radius: 4px;
    border: solid 1px lightgray;
}

.v-center {
    margin: 8px;
}

.no-title {
    font-size: small;
    text-align: center;
    color: gray;
}

.h-layout {
    display: flex;
    position: relative;
}

.btn-absolute {
    position: absolute;
    right: 4px;
    bottom: 4px;
}

button {
    color: gray;
}

/* ここから */
.margin-zero {
    margin: 0;
}

.post-all {
    width: 100%;
    min-height: 80px;
    height: 100%;
    background-color: white;
    border-radius: 4px;
    border: solid 1px lightgray;
}

.margin-eight {
    margin: 8px;
}

.small-title {
    font-size: small;
    font-weight: bold;
}

.comment {
    width: 100%;
    min-height: 56px;
    background-color: white;
    border-radius: 4px;
    border: solid 1px lightgray;
}

.back-link {
    text-align: right;
    font-size: small;
    color: gray;
}

a {
    color: gray;
}
/* ここまで追加 */

php artisan serve で BBS アプリを起動して、localhost:8000 で確認してください。



1111 visits
Posted: Jul. 28, 2025
Update: Jul. 28, 2025

ホーム   目次