投稿削除機能を反映させたい。
解決したいこと
ここに解決したい内容を記載してください。
laravelでイベントを投稿するアプリを開発しています。
投稿削除機能を反映させたいです。
発生している問題・エラー
The DELETE method is not supported for route posts/3. Supported methods: GET, HEAD.
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
以下がshow.blade.phpです。
<x-app-layout>
<x-slot name="header">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Posts</title>
<!-- Fonts -->
</x-slot>
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
<div class="px-36 py-9">
<h1 class="title px-36 py-9 my-8 text-2xl">
{{ $group->name }}
</h1>
<div class="content text-xl">
<div class="content__post">
<h3>本文</h3>
<p>{{ $group->body }}</p>
</div>
</div>
<div class="border-2 border-blue-500 rounded-md bg-blue-400">
<a href='/posts/event/{{$group->id}}'>イベントを作る</a>
</div>
{{--カレンダー機能--}}
<div class="border border-gray-300 rounded-lg">
<div class="grid grid-cols-7 gap-2">
<div class="container md">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header bg-gray-200 text-gray-600 p-2 text-3xl">
{{ $calendar->getTitle() }}
</div>
<div class="card-body bg-gray-200 text-gray-600 p-2 text-3xl text-center">
{!! $calendar->render() !!}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="text-xl">
@foreach($events as $event)
<h1 class="title">
{{ $event->title }}
</h1>
<div class="content">
<div class="content__post">
<p>{{ $event->body }}</p>
</div>
</div>
<!-- イベント投稿削除機能 -->
<form action="/posts/{{ $event->id }}" id="form_{{ $event->id }}" method="post">
@csrf
@method('DELETE')
<button type="button" onclick="deletePost({{ $event->id }})">削除する</button>
</form>
@endforeach
</div>
<script>
function deletePost(id) {
'use strict'
if (confirm('削除すると復元できません。\n本当に削除しますか?')) {
document.getElementById(`form_${id}`).submit();
}
}
</script>
<div class="footer">
<a href="/">戻る</a>
</div>
</div>
</x-app-layout>
以下がweb.phpです。
<?php
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
use App\Helper\helpers;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::get('/', [PostController::class, 'index'])->name('index');
Route::get('/create', [PostController::class, 'create'])->name('create');
Route::get('/participation', [PostController::class, 'participation'])->name('participation');
Route::get('/posts', [PostController::class, 'participation'])->name('participation.index');
Route::get('/posts/event/{group}', [PostController::class, 'event']);
Route::get('/posts/{group}', [PostController::class ,'show']);
Route::get('/posts/{event}/event_show', [PostController::class ,'event_show']);
Route::post('/groups', [PostController::class, 'store']);
Route::post('/posts/event/{group}/save', [PostController::class, 'save']);
Route::post('/posts', [PostController::class, 'join'])->name('participation.join');
Route::delete('/posts/show/{event}/delete', [PostController::class, 'delete'])->name('posts.delete');
});
//消さない!
require __DIR__.'/auth.php';
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
以下がPostController.phpです。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Group;
use App\Models\Event;
use Illuminate\Support\Facades\Auth;
use App\Calendar\CalendarView;
class PostController extends Controller
{
public function index()
{
return view('posts/index');
}
public function create()
{
return view('posts/create');
}
public function participation(Group $group)
{
return view('posts/participation')->with(['groups' => $group->get()]);;
}
public function store(Request $request, Group $group)
{
$input = $request['post'];
$group->fill($input);
$group->leader_id = Auth::user()->id;
$group->save();
$group->users()->attach(Auth::user()->id);
return redirect('\posts');
}
public function join(Request $request)
{
$group_id = $request->group_id;
$user_id = auth()->id();
$group = Group::findOrFail($group_id);
$group->users()->attach($user_id);
return redirect()->back()->with(['success'=>'グループに参加しました!']);
}
public function show(Group $group)
{
$events = $group->events()->get();
$calendar = new CalendarView(time());
return view('posts/show')->with(['group' => $group, "events" => $events, "calendar" => $calendar]);
}
public function event_show(Event $event)
{
return view('posts/show')->with(['event' => $event]);
}
public function event(Group $group)
{
return view('posts/event')->with(['group' => $group]);
}
public function save(Request $request, Event $event, Group $group)
{
$input = $request['post'];
$event->fill($input);
$event->group_id = $group->id;
$event->creator_id = Auth::user()->id;
$event->save();
$event->users()->attach(Auth::user()->id);
return redirect('/posts');
}
public function delete(Event $event)
{
$event = Event::findOrFail($id);
$event->delete();
return redirect('/');
}
}
自分で試したこと
ルーティングや、関数の定義に間違えはないかの確認をしてみたのですが私の力不足で解決できませんでした。大変恐縮ですが教えていただけると幸いです。
0