Laravel | BBS | Delete の実装

ホーム  

概要

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

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


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


Post コントローラーの変更

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

<?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([
            'title'=>'required',
            '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');
    }
    // ここまで追加します
}


Comment コントローラーの変更

app / Http / Controllers の中に CommentController.php を次のように変更します。

<?php

namespace App\Http\Controllers;

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

class CommentController extends Controller
{
    public function store(Request $request, Post $post)
    {
        $request->validate([
            'body'=>'required',
        ]);

        $comment = new Comment();
        $comment->body = $request->body;
        $comment->post_id = $post->id;
        $comment->save();

        return redirect()->route('posts.show', $post);
    }
    
    // ここから
    public function destroy(Post $post, Comment $comment)
    {
        $comment->delete();

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

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



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

ホーム   目次