기존에 작성했던 라우터

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
29
30
31
32
33
34
#|--------------------------------------------------------------------------
#| 게시판 메인가기
#|--------------------------------------------------------------------------
Route::get('/posts','PostsController@index');
 
#|--------------------------------------------------------------------------
#| 게시판 글쓰러가기
#|--------------------------------------------------------------------------
Route::get('/posts/create','PostsController@create');
 
#|--------------------------------------------------------------------------
#| 게시판 글데이터 저장하기 post데이터 전송
#|--------------------------------------------------------------------------
Route::post('/posts','PostsController@store');
 
#|--------------------------------------------------------------------------
#| 게시판 글 보기 
#|--------------------------------------------------------------------------
Route::get('/posts/{post}','PostsController@show');
 
#|--------------------------------------------------------------------------
#| 게시판 글 수정하기
#|--------------------------------------------------------------------------
Route::get('/posts/{post}/edit','PostsController@edit');
 
#|--------------------------------------------------------------------------
#| 게시판 글 수정 처리하기
#|--------------------------------------------------------------------------
Route::PATCH('posts/{post}','PostsController@update');
 
#|--------------------------------------------------------------------------
#| 게시판 글 삭제 처리하기
#|--------------------------------------------------------------------------
Route::DELETE('posts/{post}','PostsController@destroy');
cs




위 많은 소스가 이 한줄로 요약이 가능하다.

1
Route::resource('/posts','PostsController');
cs


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


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


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


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


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



게시판 목록에서 글을 클릭했을 때 자세히 보기를 구현하겠다.


web.php에서 라우터설정을 해준다.

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




resources/views/posts/show.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
25
26
27
28
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>게시판 목록</h1>
    <ul>
 
        @foreach($posts as $post)
        
        <li>
            <a href="/posts/{{$post->id}}">
                {{ $post->title }}
            </a>
        </li>
 
        @endforeach
 
    </ul>
 
    <form method="get" action="/posts/create">
        <button type="submit">
            글 작성하기
        </button>
    </form>
</body>
</html>
cs





PostsController.php show() 메서드에서 다음과 같이 작성해주자

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




resources/views/posts/show.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>
    <div>
 
        {{ $post->title }}
    </div>
    <div>
 
        {{ $post->description }}
    </div>
 
    <div>
        
        <a href="/posts/{{ $post->id }}/edit">수정하기</a>
    </div>
 
</body>
</html>
cs



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


글쓰러가기 라우터설정을 해주자(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




게시판을 목록을 만들어보자


테이블을 만들기 위해 마이그레이션 파일을 만들자

1
php artisan make:migration cerate_posts_table --create=posts
cs



database/migrations/TIMESTAMP_create_posets_table.php 파일 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CeratePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            
            $table->increments('id');
 
            $table->string('title');
 
            $table->text('description');
 
            $table->timestamps();
        });
    }
 
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}
cs


마이그레이션 실행

1
php artisan migrate
cs



컨트롤러 모델생성

1
php artisan make:controller PostsController  -r -m Post
cs


게시판 목록 페이지로 가는 라우터 설정 (web.php)

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




index메서드에 뷰 지정해주기 (PostsController.php)

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




뷰파일 만들어주기(resources/views/posts/index.blade.php

1
2
3
4
5
6
7
8
<!DOCTYPE html><html>
<head>
    <title></title>
</head>
<body>
    <h1>게시판 목록</h1>
</body>
</html>
cs




데이터베이스에 테이블에 데이터가 비어있으니 팅커콘솔로 인서트해주자.

모델로 가서 (Post.php) 대량할당이 가능하도록 해주자.

1
2
3
4
class Post extends Model
{
    protected $fillable = ['title','description'];
}
cs




그다음 터미널에서 php artisan tinker을 입력해서 팅커콘솔로 접속하여 데이터를 임의로 몇개 삽입하여준다.

1
2
3
4
App\Post::create([
'title' => 'title1',
'description' => 'description'
]);
cs





PostsController에서 소스를 작성해보자. 데이터베이스의 데이터를 불러올 것이다.

1
2
3
4
5
6
7
8
    public function index()
    {
       $posts = \App\Post::all(); 
 
       return $posts;
  
    }
cs


이제 웹브라우저에서 잘 불러오는지 확인해보자.

http://domain/posts




데이터가 불러오니 이제 view단에 데이터를 위임할 것이다. (PostsController)

1
2
3
4
5
6
    public function index()
    {
       $posts = \App\Post::all(); 
 
       return view('posts.index',compact('posts')); 
    }
cs




생략/views/posts/index.blade.php 파일에서 반복문을 작성해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>게시판 목록</h1>
    <ul>
 
        @foreach($posts as $post)
            <li>{{ $post->title }}</li>
        @endforeach
 
    </ul>
</body>
</html>
cs




성공적으로 게시판 목록이 불러와졌다.


글 목록을 만들고,

글 쓰기 기능을 만들고,

글 수정 기능을 만들고,

글 삭제 기능을 만들고,

글 보기 기능을 만든다.


GET /projects (index)


GET /projects/create (index)


GET /projects/1 (show)


POST /projects (store)


GET /projects/1/edit (edit)


PATCH /projects/1 (update)


DELETE /projects/1 (destroy)



routes/web.php

1
Route::resource('/projects','ProjectsController');
cs



ProjectsController.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Project;
 
class ProjectsController extends Controller
{
 
 
#|--------------------------------------------------------------------------
#| 모델에서 모든 데이터를 가져와서 projects.index에 던져준다..
#|--------------------------------------------------------------------------    
    public function index()
    {
        $projects = \App\Project::all();
 
        return view('projects.index', compact('projects'));
    }
 
#|--------------------------------------------------------------------------
#| 해당 글을 작성하는 뷰를 반한한다.
#|--------------------------------------------------------------------------    
 
    public function store()
    {
        Project::create(request(['title''description']));
        
        return redirect('/projects');
    }
 
 
#|--------------------------------------------------------------------------
#| 해당 글을 작성하는 뷰를 반한한다.
#|--------------------------------------------------------------------------
    public function create()
    {
        return view('projects.create');
    }
 
 
#|--------------------------------------------------------------------------
#| 해당 글을 목록에서 눌렀을 때 projects.show 뷰에 던져준다.
#|--------------------------------------------------------------------------    
    public function show(Project $project)
    {
        return view('projects.show', compact('project'));
    }
    
#|--------------------------------------------------------------------------
#| 해당 글을 가져와서 projects.edit 뷰에 던져준다.
#|--------------------------------------------------------------------------
    public function edit(Project $project)
    {    
        //$project = Project::findOrFail($id);
        return view('projects.edit', compact('project'));
    }
 
 
#|--------------------------------------------------------------------------
#| projects.edit에서 수정한 글을 업데이트 하는 곳
#|--------------------------------------------------------------------------
    public function update(Project $project)
    {
        $project->update(request(['title','description']));
 
        return redirect('/projects');
    }
 
#|--------------------------------------------------------------------------
#| projects.edit에서 삭제를 클릭하면 실행되는 로직
#|--------------------------------------------------------------------------
    public function destroy(Project $project)
    {
        $project->delete();
        //dd('delete ' . $id);
        return redirect('/projects');
    }
 
}
 
cs


views/projects 에 다음과 같은 파일을 생성한다.


layout.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
    <title></title>
 
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.css">
</head>
<body>
 
    <div class="container">
        
        @yield('content')
 
    </div>
 
</body>
</html>
cs


projects/index.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@extends('layout')
 
@section('content')
 
<h1>글 목록</h1>
 
<ul>
    @foreach($projects as $project)
 
    <li>
        <a href="/projects/{{ $project->id }}">
            {{ $project->title }}
        </a>
    </li>
 
    @endforeach
</ul>
 
@endsection
cs




projects/create.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@extends('layout')
 
@section('content')
 
    <h1>글 작성하기</h1>
 
    <form method="POST" action="/projects">
            @csrf
        <input type="text" name="title" placeholder="제목"><br>
 
        <textarea name="description" placeholder="내용"></textarea>
 
        <br>
        <button type="submit">
            글쓰기
        </button>    
    </form>
 
@endsection
cs



projects/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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@extends('layout')
 
@section('content')
 
<h1 class="title">글 수정하기</h1>
 
<form method="POST" action="/projects/{{ $project->id }}" style="margin-bottom: 1em">
    @method('PATCH')
    {{-- {{ method_field('PATCH') }} --}}
    @csrf
 
<div class="field">
    <lable class="lable" for="title">Title</lable>
 
    <div class="control">
 
        <input class="input" type="text" name="title" placeholder="제목" value="{{ $project->title }}">
    </div>
 
</div>
 
 
<div class="field">
    <lable class="lable">description</lable>
 
    <div class="control">
        
        <textarea name="description" class="textarea">{{ $project->description }}</textarea>
    </div>
</div>
 
<div class="field">
    <div class="control">
        
        <button class="button is-link" type="submit">글 작성</button>
    </div>
</div>
 
</form>
 
<form method="POST" action="/projects/{{ $project->id }}">
    @method('DELETE')
    @csrf
 
<div class="field">
    <div class="control">
        
        <button class="button" type="submit">글 삭제</button>
    </div>
</div>
 
</form>
 
@endsection    
 
 
cs



projects/show.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@extends('layout')
 
@section('content')
<h1 class="title">글 상세</h1>
 
 
<li>{{ $project->title }}</li>
 
<div class="content">{{ $project->description }}</div>
 
<p>
    <a href="/projects/{{ $project->id }}/edit">수정하기</a>
</p>
 
 
@endsection
cs










419에러

form에서 post방식으로 넘겼을 때 csrf토근값이 없으면 에러가 나온다.

form 아래에 @csrf 를 삽입한다.  또는 {{ csrf_field() }}

 

컨트롤러

	public function store()
	{
		return request()->all();
	}

+ Recent posts