Laravel | BBS | Update の実装

ホーム  

概要

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

WEB アプリケーションには、CRUD という機能をつけると良いとされています。 CRUD とは、Create (作成)、Read (読み込み)、Update (更新)、Delete (削除)の頭文字をとったものです。 今回は、Update (更新) 機能をつけていきます。


Show ページの変更

resources / views /posts の 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 class="spc">
<div>
<h1 class="margin-zero">Show</h1>
</div>
<div>
<form method="POST" action="{{ route('posts.destroy', $post) }}">
    @method('delete')
    @csrf
    <button class="margin-zero" onclick="return confirm('Sure?');">Delte post</button>
    </form>
</div>
</div>
    <ul>
    {{-- ここから --}}
    <li class="h-layout">
        <div class="post-all">
            <p class="margin-eight">{{$post->body}}</p>
        </div>
        <div>
            <form method="GET" action="{{ route('posts.edit', $post) }}">
            @csrf
            <button class="btn-absolute">Edit post</button>
            </form>
        </div>
    </li>
    {{-- ここまで変更 --}}
    </ul>

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

    <ul>
    @forelse ($post->comments as $comment)
    <li class="h-layout">
    <div class="comment">{{ $comment->body }}</div>
    <div>
    <form method="POST" action="{{ route('posts.comments.destroy', [$post, $comment])}}">
    @csrf
    @method('delete')
    <button class="btn-absolute" onclick="return confirm('Sure?');">Delete comment</button>
    </form></div></li>
    @empty
    <li class="no-title">No comment.</li>
    @endforelse
    </ul>
    <form method="POST" action="{{ route('posts.comments.store', $post) }}">
    @csrf
    <div>
    <input type="text" name="body">
    @error('body')
        <p class="error">{{ $message }}</p>
    @enderror
    </div>
    <div class="a-right">
        <button class="margin-top">Create comment</button>
    </div>
    </form>
    <p class="back-link"><a href="{{route('posts.index')}}">Back to Index</a></p>
    </div>
</body>
</html>


Edit ページの作成

resources / views / posts の中に 下記の内容の edit.blade.php というファイルを作ってください。

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit | BBS</title>
    <link rel="stylesheet" href="{{ url('style.css') }}">
</head>
<body>
    <div class="container">
    <div class="spc">
    <div>
    <h1 class="margin-zero">Edit</h1>
    </div>
    <div>
    <form method="POST" action="{{ route('posts.update', $post) }}">
    @method('patch')
    @csrf
    <button clas="margin-top">Update</button>
    </div>
    </div>
    <div>
        <textarea name="body">{{ old('body', $post->body) }}</textarea>
        @error('body')
        <p class="error">{{ $message }}</p>
        @enderror
    </div>
    </form>
    <p class="back-link"><a href="{{ route('posts.show', $post) }}">Back to Show</a></p>
    </div>
</body>
</html>



Post コントローラーの変更

app / Http / Controllers の PostController.php に update メソッドと update メソッドを追加します。

<?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]);
    }
    
    public function create()
    {
        return view('posts.create');
    }
    
    public function store(Request $request)
    {
        $request->validate([
            'body'=>'required',
        ]);
        
        $post = new Post();
        $post->title = $request->title;
        $post->body = $request->body;
        $post->save();

        return redirect()->route('posts.index');
    }
    
    public function destroy(Post $post)
    {
        $post->delete();
        return redirect()->route('posts.index');
    }
    
    // ここから
    public function edit(Post $post)
    {
        return view('posts.edit')->with(['post'=>$post]);
    }

    public function update(Request $request, Post $post)
    {
        $post->body = $request->body;
        $post->save();

        return redirect()->route('posts.show', $post);
    }
    // ここまで追加
}

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

もし、サーバーの準備ができていたら、アプリケーションをサーバーにデプロイする で、作ったアプリケーションを本番サーバーに設置する方法を説明しています。



1117 visits
Posted: Jul. 29, 2025
Update: Jul. 29, 2025

ホーム   目次