Laravel まとめ4(記事編集)

#記事編集まで
(#20.#21)
http://dotinstall.com/lessons/basic_laravel/36121

#ルート

```/myblog/app/Http/routes.php

<?php

Route::get('/', 'PostsController@index');
Route::get('/posts/create','PostsController@create');
Route::get('/posts/{id}', 'PostsController@show');

//追加
Route::get('/posts/{id}/edit','PostsController@edit');
Route::post('/posts', 'PostsController@store');

//追加
Route::patch('/posts/{id}','PostsController@update');
```

- Route::patch() の場合は PostsController@update を呼ぶ


#Controller

```/myblog/app/Http/Controllers/PostsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Post;

use App\Http\Requests\PostRequest;

class PostsController extends Controller
{
    public function index() {
      $posts = Post::latest('created_at')->get();
      return view('posts.index')->with('posts', $posts);
    }

    public function show($id) {
      $post = Post::findOrFail($id);
      return view('posts.show')->with('post', $post);
    }

//追加
    public function edit($id) {
      $post = Post::findOrFail($id);
      return view('posts.edit')->with('post', $post);
    }

    public function create() {
      return view('posts.create');
    }

    public function store(PostRequest $request) {
      // $this->validate($request, [
      //   'title' => 'required|min:3',
      //   'body' => 'required'
      // ]);
      $post = new Post();
      $post->title = $request->title;
      $post->body = $request->body;
      $post->save();
      return redirect('/')->with('flash_message', 'Post Added!');
    }

    public function update(PostRequest $request, $id) {
      // $this->validate($request, [
      //   'title' => 'required|min:3',
      //   'body' => 'required'
      // ]);
      $post = Post::findOrFail($id);
      $post->title = $request->title;
      $post->body = $request->body;
      $post->save();
      return redirect('/')->with('flash_message', 'Post Updated!');
    }

}
```

- validationを外に出す

```
php artisan make:request PostRequest
```

```/myblog/app/Http/Requests/PostRequest.php 

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class PostRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // return false;

//ここをtrueにする
        return true;
    }

//ルールをここに書く
    public function rules()
    {
        return [
          'title' => 'required|min:3',
          'body' => 'required'
        ];
    }
}
```

- 認証の仕組みは無いので、何でも通すよという意味で true にしておく。


#View

- edit.blade.php

```/myblog/resources/views/posts/edit.blade.php


@extends('layouts.default')

@section('title', 'Edit Post')

@section('content')
<h1>
  <a href="{{ url('/') }}" class="pull-right fs12">Back</a>
  Edit Post
</h1>
<form method="post" action="{{ url('/posts', $post->id) }}">
  {{ csrf_field() }}
  {{ method_field('patch') }}
  <p>
    <input type="text" name="title" placeholder="title" value="{{ old('title', $post->title) }}">
    @if ($errors->has('title'))
    <span class="error">{{ $errors->first('title') }}</span>
    @endif
  </p>
  <p>
    <textarea name="body" placeholder="body">{{ old('body', $post->body) }}</textarea>
    @if ($errors->has('body'))
    <span class="error">{{ $errors->first('body') }}</span>
    @endif
  </p>
  <p>
    <input type="submit" value="Update">
  </p>
</form>
@endsection
```

```
・ patchメソッドで渡す
・ フォームの表示は「$post->title」「$post->body」
・ Validationの後はoldを表示
・ →oldのtitle、bodyが無かったら 「$post->title」「$post->body」 を表示

```

[表示]

![スクリーンショット 2016-07-21 17.12.01.png](https://qiita-image-store.s3.amazonaws.com/0/62264/14e462ba-9124-0695-196f-5ad2a1b826b6.png)

![スクリーンショット 2016-07-21 17.12.18.png](https://qiita-image-store.s3.amazonaws.com/0/62264/bf2b8b01-402a-612f-d72b-0bb99a72aedb.png)

![スクリーンショット 2016-07-21 17.12.27.png](https://qiita-image-store.s3.amazonaws.com/0/62264/ce72d19a-ee38-f778-f08b-1b99a6fa4f33.png)