Laravel まとめ3(バリデーション)

#バリデーションまで
(#16〜19)
http://dotinstall.com/lessons/basic_laravel/36119
http://bit.ly/29EvNEA

#ルート

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

<?php

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

- /postsにpostで飛んできたら、　postsコントローラーのstoreメソッドを呼ぶ

#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;

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 create() {
      return view('posts.create');
    }

    public function store(Request $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!');
    }
}
```

バリデーションロジック
http://readouble.com/laravel/5/1/ja/validation.html

- 'required|min:3' 3文字


#View

###default.blade.php

```/myblog/resources/views/layouts/default.blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>@yield('title')</title>
  <link rel="stylesheet" href="/css/styles.css">
</head>
<body>
  @if (session('flash_message'))
  <div class="flash_message" onclick="this.classList.add('hidden')">{{ session('flash_message') }}</div>
  @endif
  <div class="container">
    @yield('content')
  </div>
</body>
</html>
```

###create.blade.php

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

@extends('layouts.default')

@section('title', 'Add New')

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

```
- oldヘルパー(直前に入力された値を表示) フォーム内にバリデーション後も値を表示する為


###index.blade.php

- Add Newボタンの追加

```
@extends('layouts.default')

@section('title', 'Blog Posts')

@section('content')
<h1>
  <a href="{{ url('/posts/create') }}" class="pull-right fs12">Add New</a>
  Posts
</h1>
<ul>
  @forelse ($posts as $post)
  <li><a href="{{ action('PostsController@show', $post->id) }}">{{ $post->title }}</a></li>
  @empty
  <li>No posts yet</li>
  @endforelse
</ul>

@endsection

```

###show.blade.php

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

@extends('layouts.default')

@section('title', 'Blog Detail')

@section('content')
<h1>
  <a href="{{ url('/') }}" class="pull-right fs12">Back</a>
  {{ $post->title }}
</h1>
<p>{!! nl2br(e($post->body)) !!}</p>
@endsection

```

#表示

![1.png](https://qiita-image-store.s3.amazonaws.com/0/62264/f9ae930e-098f-1fc7-91ae-48960aae258f.png)

![2.png](https://qiita-image-store.s3.amazonaws.com/0/62264/c15d924d-9e33-5699-925e-cc32a5e82801.png)

![3.png](https://qiita-image-store.s3.amazonaws.com/0/62264/344ff5ed-96c3-f41e-52e7-f0c3ae55641a.png)