이제 삭제기능을 만들어보자


글 자세히보기에서 삭제버튼을 달아보자


resources/views/posts/show.blade.php 해당 소스를 추가하자

1
2
3
4
5
<form method="POST" action="/posts/{{ $post->id }}">
    @method('DELETE')
    @csrf
    <button type="submit">글 삭제</button>    
</form>
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>글 상세페이지</h1>
    <div>
 
        {{ $post->title }}
    </div>
    <div>
 
        {{ $post->description }}
    </div>
 
    <div>
        <a href="/posts/{{ $post->id }}/edit">수정하기</a>
    </div>
 
<form method="POST" action="/posts/{{ $post->id }}">
    @method('DELETE')
    @csrf
    <button type="submit">글 삭제</button>    
</form>
 
</body>
</html>
cs




web.php에서 글 삭제 라우터를 등록해주자

1
Route::DELETE('posts/{post}','PostsController@destroy');
cs



글을 삭제하는 로직을 PostsController.php에서 소스작성을 해주자

1
2
3
4
5
6
    public function destroy(Post $post)
    {
       $post->delete();
 
       return redirect('/posts');
    }
cs


+ Recent posts