이제 글쓰기 페이지를 만들어보자


글쓰러가기 라우터설정을 해주자(web.php)

1
Route::get('/posts/create','PostsController@create');

cs






PostsController create() 메서드를 작성해주자

1
2
3
4
    public function create()
    {
        return view('posts.create');
    }
cs




resources/views/posts/create.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
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>글 작성</h1>
    <form method="POST" action="/posts">
  @csrf
        <div>
            <input type="text" name="title" placeholder="제목">
        </div>
 
        <div>
            <textarea name="description" placeholder="내용"></textarea>    
        </div>
 
        <div>
            <button type="submit">글 작성</button>
        </div>
    </form>
</body>
</html>
cs





PostsController store()메서드 소스작성해주기

1
2
3
4
    public function store(Request $request)
    {
        dd($request);
    }
cs


글 작성페이지에서 글을 작성해보자.




PostsController store()메서드에 유효성검사와 데이터를 삽입하는 소스를 작성한다.

그리고 다시 목록으로 리다이렉트시킨다.

1
2
3
4
5
6
7
8
9
    public function store()
    {
        Post::create(request()->validate([
            'title' => ['required','min:3'],
            'description' => ['required','min:3']
        ]));
 
        return redirect('/posts');
    }
cs




+ Recent posts