validationを纏めるファイル

[vagrant@localhost laravel]$ php artisan make:request ArticleRequest
Request created successfully.

サーバーを再起動
php artisan serve –host 192.168.35.10 –port 8000

appの中のhttpの中にできる

ArticleRequest

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ArticleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
            'login_id' => 'required|min:3',
            'role' => 'required|min:2',
            'name' => 'required|min:2',
            'password' => 'required',
            'mail' => 'required',
            'test_mail' => 'required',
            'updated_person' => 'required|min:3'
        ];
    }
    public function messages(){
        return [
            ''
        ];
    }
}

updateのcontroller

public function update(Request $request, Article $article){
    	$this->validate($request, [
    		'login_id' => 'required|min:3',
    		'role' => 'required|min:2',
    		'name' => 'required|min:2',
    		'password' => 'required',
    		'mail' => 'required',
    		'test_mail' => 'required',
    		'updated_person' => 'required|min:3'
    	]);
    	$article->login_id = $request->login_id;
    	$article->role = $request->role;
    	$article->name = $request->name;
    	$article->password = $request->password;
    	$article->mail = $request->mail;
    	$article->test_mail = $request->test_mail;
    	$article->updated_person = $request->updated_person;
    	$article->save();
    	return redirect('/');
    }

渡されたメソッドのupdate

なるほど~

edit

@extends('layouts.default')

@section('title', '編集')

@section('content')
<h1><a href="/">登録情報</a></h1>
	<h3>編集</h3>
	<form method="post" action="{{ url('/articles', $article->id) }}">
		{{ csrf_field()}}
		{{ method_field('patch') }}
	<p>
		<input type="text" name="login_id" placeholder="ログインID" value="{{ old('login_id', $article->login_id) }}">
		@if($errors->has('login_id'))
		<span class="error">{{ $errors->first('login_id') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="role" placeholder="権限" value="{{ old('role', $article->role) }}">
		@if($errors->has('role'))
		<span class="error">{{ $errors->first('role') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="name" placeholder="氏名" value="{{ old('name', $article->name) }}">
		@if($errors->has('name'))
		<span class="error">{{ $errors->first('name') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="password" placeholder="パスワード" value="{{ old('password', $article->password) }}">
		@if($errors->has('password'))
		<span class="error">{{ $errors->first('password') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="mail" placeholder="メールアドレス" value="{{ old('mail', $article->mail) }}">
		@if($errors->has('mail'))
		<span class="error">{{ $errors->first('mail') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="test_mail" placeholder="テストメール" value="{{ old('test_mail', $article->test_mail) }}">
		@if($errors->has('test_mail'))
		<span class="error">{{ $errors->first('test_mail') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="updated_person" placeholder="更新者氏名" value="{{ old('updated_person', $article->updated_person) }}">
		@if($errors->has('updated_person'))
		<span class="error">{{ $errors->first('updated_person') }}</span>
		@endif
	</p>
	<p>
		<input type="submit" value="更新する">
	</p>
	</form>
@endsection

routing

Route::get('/', 'ArticlesController@index');
Route::get('/articles/create', 'ArticlesController@create');
Route::get('/articles/{id}', 'ArticlesController@show')->where('post','[0-9]+');
Route::post('/articles', 'ArticlesController@store');
Route::get('/articles/{id}/edit', 'ArticlesController@edit');

リンク
controllerにidを渡して、controllerでeditにidを渡している。
editのroutingでidを元にパスを指定。

<ul>
		@foreach ($articles as $article)
		<li>{{$article->login_id}}  <a href="{{ action('ArticlesController@show', $article->id) }}">{{ $article->name}}</a><a href="{{ action('ArticlesController@edit', $article->id)}}" class="register">編集</a></li>
		@endforeach
	</ul>

わかったような、わからんような。
凄いな、この仕組み。

laravelのvalidation

Base
sometimes
bail

存在チェック系
required
present
filled
in
not_in
in_array

型チェック
boolean
accepted

文字列判定
string
alpha
alpha_dash
alpha_num
email
url
active_url
ip
json
timezone
regex

日付判定
date
date_format
after
before

数値判定
integer
numeric
digits
digits_between

配列判定
array
distinct

大きさ判定
size
min
max
between

比較判定
gt
gte
lt
lte

ファイル判定
file
image
dimensions
mimes

同値チェック
confirmed
same
different

validation

controller

public function store(Request $request){
    	$this->validate($request, [
    		'login_id' => 'required|min:3',
    		'role' => 'required|min2',
    		'name' => 'required|min2',
    		'password' => 'required',
    		'mail' => 'required',
    		'test_mail' => 'required',
    		'updated_person' => 'required|min:3'
    	]);
    	$article = new Article();
    	$article->login_id = $request->login_id;
    	$article->role = $request->role;
    	$article->name = $request->name;
    	$article->password = $request->password;
    	$article->mail = $request->mail;
    	$article->test_mail = $request->test_mail;
    	$article->updated_person = $request->updated_person;
    	$article->save();
    	return redirect('/');
    }

create.blade.php

@extends('layouts.default')

@section('title', '新規登録')

@section('content')
<h1><a href="/">登録情報</a></h1>
	<h3>新規登録</h3>
	<form method="post" action="{{ url('/articles') }}">
		{{ csrf_field()}}
	<p>
		<input type="text" name="login_id" placeholder="ログインID" value="{{ old('login_id') }}">
		@if($errors->has('login_id'))
		<span class="error">{{ $errors->first('login_id') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="role" placeholder="権限" value="{{ old('role') }}">
		@if($errors->has('role'))
		<span class="error">{{ $errors->first('role') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="name" placeholder="氏名" value="{{ old('name') }}">
		@if($errors->has('name'))
		<span class="error">{{ $errors->first('name') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="password" placeholder="パスワード" value="{{ old('password') }}">
		@if($errors->has('password'))
		<span class="error">{{ $errors->first('password') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="mail" placeholder="メールアドレス" value="{{ old('mail') }}">
		@if($errors->has('mail'))
		<span class="error">{{ $errors->first('mail') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="test_mail" placeholder="テストメール" value="{{ old('test_mail') }}">
		@if($errors->has('test_mail'))
		<span class="error">{{ $errors->first('test_mail') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="updated_person" placeholder="更新者氏名" value="{{ old('updated_person') }}">
		@if($errors->has('updated_person'))
		<span class="error">{{ $errors->first('updated_person') }}</span>
		@endif
	</p>
	<p>
		<input type="submit" value="登録">
	</p>
	</form>
@endsection

form
WoW!

postのvalidationを設定しよう

public function store(Request $request){
    	$this->validate($request, [
    		'login_id' => 'required|min:3',
    		'role' => 'required|min2',
    		'name' => 'required|min2',
    		'password' => 'required',
    		'mail' => 'required',
    		'test_mail' => 'required',
    		'updated_person' => 'required|min:3'
    	]);
    	$article = new Article();
    	$article->login_id = $request->login_id;
    	$article->role = $request->role;
    	$article->name = $request->name;
    	$article->password = $request->password;
    	$article->mail = $request->mail;
    	$article->test_mail = $request->test_mail;
    	$article->updated_person = $request->updated_person;
    	$article->save();
    	return redirect('/');
    }

password, メールアドレスのバリデーションがよくわらんね。

新規登録ページを作成していこう

まずrouting
createをつくります。

Route::get('/', 'ArticlesController@index');
Route::get('/articles/create', 'ArticlesController@create');
Route::get('/articles/{id}', 'ArticlesController@show')->where('post','[0-9]+');
public function create(){
    	return view('articles.create');
    }

create.blade.php

@extends('layouts.default')

@section('title', '新規登録')

@section('content')
<h1><a href="/">登録情報</a></h1>
	<h3>新規登録</h3>
	<form method="post" action="{{ url('/articles') }}">
		{{ csrf_field()}}
	<p>
		<input type="text" name="login_id" placeholder="ログインID">
	</p>
	<p>
		<input type="text" name="role" placeholder="権限">
	</p>
	<p>
		<input type="text" name="name" placeholder="氏名">
	</p>
	<p>
		<input type="text" name="password" placeholder="パスワード">
	</p>
	<p>
		<input type="text" name="mail" placeholder="メールアドレス">
	</p>
	<p>
		<input type="text" name="test_mail" placeholder="テストメール">
	</p>
	<p>
		<input type="text" name="updated_person" placeholder="更新者氏名">
	</p>
	<p>
		<input type="submit" value="登録">
	</p>
	</form>
@endsection

フォームができました。

ルーティング

Route::post('/articles', 'ArticlesController@store');

入れてみよう

入った!
すげー

複数ページ作っていこう

ルーティングを追加します。

Route::get('/', 'ArticlesController@index');
Route::get('/articles/{id}', 'ArticlesController@show')

一覧

<div class="container">
	<h1>Articles</h1>
	<h3>氏名一覧</h3>
	<ul>
		@foreach ($articles as $article)
		<li>{{$article->login_id}}  <a href="{{ action('ArticlesController@show', $article->id) }}">{{ $article->name}}</a></li>
		@endforeach
	</ul>
  </div>

詳細

<head>
	<meta charset="utf-8">
	<title>{{ $article->name}}</title>
	<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
  <div class="container">
	<h1><a href="/">登録情報</a></h1>
	<h3>{{ $article->name}}</h3>
		<p>{{$article->login_id}}</p>
		<p>{{$article->name}}</p>
		<p>{{$article->role}}</p>
		<p>{{$article->password}}</p>
  </div>
</body>

おう、なんとなく詳細はできたか。。

default.bladeをつくっていく。

<!DOCTYPE>
<html>
<head>
	<meta charset="utf-8">
	<title>@yield('title')</title>
	<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
  <div class="container">
	@yield('content')
  </div>
</body>
</html>

ほうほう、

controllerとviewの操作を続けよう

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Article;

class ArticlesController extends Controller
{
    //
    public function index(){
    	$articles = Article::all();
    	dd($articles->toArray());
    	return view('articles.index');
    }
}

<ul>
		@foreach ($articles as $article)
		<li><a href="">{{ $article->name }}</a></li>
		@endforeach
	</ul>

ほうほう、少しわかってきたぞー

<h1>Articles</h1>
	<ul>
		@foreach ($articles as $article)
		<li><a href="">{{ $article->name }}</a></li>
		@endforeach
	</ul>

laravelでcontrollerをつくっていこう

まずrouting

routesフォルダからweb.phpを開きます。ArticlesControllerを指定。

Route::get('/', 'ArticlesController@index');

make:controller
[vagrant@localhost laravel]$ php artisan make:controller ArticlesController
Controller created successfully.

app/Http/Controllersに ArticlesController.phpがつくられる。

あーなるほど。

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticlesController extends Controller
{
    //
}
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticlesController extends Controller
{
    //
    public function index(){
    	return "hello";
    }
}
class ArticlesController extends Controller
{
    //
    public function index(){
    	return view('articles.index');
    }
}
<!DOCTYPE>
<html>
<head>
	<meta charset="utf-8">
	<title>Articles</title>
</head>
<body>
	<h1>Articles</h1>
	<ul>
		<li><a href="">name1</a></li>
		<li><a href="">name2</a></li>
	</ul>
</body>
</html>

カスタマイズすると大分勝手が違うな~