글 수정페이지를 만들어보자


web.php

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




PostsController.php

1
2
3
4
    public function edit(Post $post)
    {
        return view('posts.edit',compact('post'));
    }
cs




resources/views/posts/edit.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>글 수정</h1>
    <form method="POST" action="/posts/{{ $post->id }}">
        @csrf
        @method('PATCH')
        <div>
            <input type="text" name="title"  value="{{ $post->title }}">
        </div>
 
        <div>
            <textarea name="description">{{ $post->description }}</textarea>    
        </div>
 
        <div>
            <button type="submit">글 수정</button>
        </div>
    </form>
</body>
</html>
cs





이제 글 수정한 데이터를 다시 데이터베이스에 담는 로직을 만들 것이다.

web.php 파일에 update()메서드로 가는 라우터를 설정해주자

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




PostsController.php 파일에 update()메서드를 작성하자.

1
2
3
4
5
6
    public function update(Request $request, Post $post)
    {
        $post->update(request(['title','description']));
 
        return redirect('/posts');
    }
cs



+ Recent posts