글 목록을 만들고,
글 쓰기 기능을 만들고,
글 수정 기능을 만들고,
글 삭제 기능을 만들고,
글 보기 기능을 만든다.
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 |
'라라벨' 카테고리의 다른 글
테이블명을 모델명으로 전환시켜주는 팅커콘솔 명령어 (0) | 2019.07.15 |
---|---|
타임스템프 끄는 방법 (timestamps = false) (0) | 2019.05.13 |
Composer (0) | 2019.04.16 |
데이터베이스 마이그레이션 (0) | 2019.04.02 |
엘로퀀트ORM (0) | 2019.04.01 |