git hubのrepositoriesにpushしていこう

まずは create repository

vagrantのlaravelのディレクトリに移動します。
[vagrant@localhost agile]$ cd laravel
[vagrant@localhost laravel]$ ls
app composer.json database public routes tests
artisan composer.lock package.json readme.md server.php vendor
bootstrap config phpunit.xml resources storage webpack.mix.js

githubのインストラクションに沿って操作していきます。
[vagrant@localhost laravel]$ echo “# article” >> README.md

git initします。
[vagrant@localhost laravel]$ git init
Initialized empty Git repository in /home/vagrant/agile/laravel/.git/
[vagrant@localhost laravel]$ ls
README.md composer.json package.json resources tests
app composer.lock phpunit.xml routes vendor
artisan config public server.php webpack.mix.js
bootstrap database readme.md storage
[vagrant@localhost laravel]$ git add README.md

Gitの操作の基本中の基本、変更したファイルを「addしてcommit」
つまり、Readme.mdをaddしてcommitするという訳。

続いてgit commitします。
[vagrant@localhost laravel]$ git commit -m “first commit”
[master (root-commit) b32fcda] first commit
Committer: vagrant
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config –global user.name “Your Name”
git config –global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

git commit –amend –author=’Your Name

1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README.md

[vagrant@localhost laravel]$ git remote add origin https://github.com/githubix/article.git
[vagrant@localhost laravel]$ git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/githubix/article.git/info/refs

ぬ? 403 Forbidden?
usernameを入れないと駄目のよう。
git pushします。
[vagrant@localhost laravel]$ git remote set-url origin https://githubix@github.com/githubix/article.git
[vagrant@localhost laravel]$ git push -u origin master
Password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for ‘master’ on GitHub by visiting:
remote: https://github.com/githubix/article/pull/new/master
remote:
To https://githubix@github.com/githubix/article.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

あれ、ファイルが、readme.mdだけ更新されとる。。
Untracked files:になってるからですな。

commitしてpushする。

[vagrant@localhost laravel]$ git commit -m “third commit”
[vagrant@localhost laravel]$ git add .
[vagrant@localhost laravel]$ git status

[vagrant@localhost laravel]$ git push -u origin master
Password:
Counting objects: 137, done.
Compressing objects: 100% (118/118), done.
Writing objects: 100% (135/135), 197.10 KiB, done.
Total 135 (delta 11), reused 0 (delta 0)
remote: Resolving deltas: 100% (11/11), done.
To https://githubix@github.com/githubix/article.git
b32fcda..e71a232 master -> master
Branch master set up to track remote branch master from origin.

なるほど、入りました。よし、ここはOKとしよう。

次は、awsのroute 53の複数設定。
Apacheでのマルチドメインの設定はバーチャルホストの設定だったような気がするが。。

その前にプルリクをやってみたい。

戦略

1. git hub commit
2. aws route 53複数
3. mysql -> csv
4. mysql -> S3
5. s3-> mysql …. 9/23
5-2. SSL
6. Travis CI
7. Capistrano
8. Ansible
9. Mackerel …….. 9/24
10. ELB
11. laravel bootstrap
12. laravel TinyMCE …. 9/25
13. Vue.js ….9/27
14. sketch …9/28
15. mysql設計書 9/29
16. シェルスクリプトによるbatch …9/30
17. 設計(TR,概念図)
18. struts …10/1
19. TD …10/2
20. Fluentd / Embulk …10/3
21. 設計(TR,概念図)
22. Dancer …10/4
23. 提案書作成 …10/5
24. Zabbix
25. Oracle
その他: photoshop, illustrator、python, cdn, postfix, redmine

よし!行けるかな。。。。 いや、これで行ってみよう!

Document入力

show.blade.php
DocumentsController@storeにpostする。

<form method="post" action="{{ action('DocumentsController@store', $article) }}">
		{{ csrf_field()}}
	<p>
		<input type="text" name="login_id" placeholder="iphone/android" value="{{ old('mobile') }}">
		@if($errors->has('mobile'))
		<span class="error">{{ $errors->first('mobile') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="role" placeholder="配信日" value="{{ old('published_at') }}">
		@if($errors->has('published_at'))
		<span class="error">{{ $errors->first('published_at') }}</span>
		@endif
	</p>
	<p>
		<input type="text" name="name" placeholder="原稿" value="{{ old('body') }}">
		@if($errors->has('body'))
		<span class="error">{{ $errors->first('body') }}</span>
		@endif
	</p>
	<p>
		<input type="submit" value="登録">
	</p>
	</form>

routing
DocumentsController@storeを/articles/{article}/documentsと設定する。

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');
Route::patch('/articles/{article}', 'ArticlesController@update');
Route::delete('/articles/{article}', 'ArticlesController@destroy');
Route::post('/articles/{article}/documents', 'DocuumentsController@store');

mysqlにも値が入りました。
mysql> select * from documents;
+—-+————+——–+————–+——–+———————+———————+
| id | article_id | mobile | published_at | body | created_at | updated_at |
+—-+————+——–+————–+——–+———————+———————+
| 1 | 10 | iphone | 9/12 | aaaaaa | 2018-09-23 08:46:45 | 2018-09-23 08:46:45 |
+—-+————+——–+————–+——–+———————+———————+
1 row in set (0.00 sec)

テーブルを紐づける

[vagrant@localhost laravel]$ php artisan make:model Document –migration
Model created successfully.
Created Migration: 2018_09_22_220524_create_documents_table

public function up()
    {
        Schema::create('documents', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('article_id'); // articleのid
            $table->string('body');
            $table->timestamps();
            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
        });
    }

コーディングと設計は使う脳みそが違いますね。先に設計をつくっておかないと、思いつきの適当なサービスになってしまいますな。
dateが型がよくわかりません。timestampsだと、yyyymmdd hh:mm:ssになりそうで。

public function up()
    {
        Schema::create('documents', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('article_id'); // articleのid
            $table->string('mobile');
            $table->string('published_at');
            $table->text('body');
            $table->timestamps();
            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
        });
    }

migrateします。
[vagrant@localhost laravel]$ php artisan migrate
Migrating: 2018_09_22_220524_create_documents_table
Migrated: 2018_09_22_220524_create_documents_table

belong to

namespace App;

use Illuminate\Database\Eloquent\Model;

class Document extends Model
{
    //
    protected $fillable = ['mobile','published_at','body'];

    public function article(){
    	return $this->belongsTo('App\Article')
    }

}

article.phpの方

class Article extends Model
{
    //
    protected $fillable = ['login_id','role','name','password','mail','test_mail','updated_person'];

    public function documments(){
    	return $this->hasMany('App\Document')
    }
}

紐づけはできたっぽい
いけるもんだ。

delete処理

なんかうまくいかねーなーと思ってたら、jsのリンク間違ってた。。。

(function() {
  'use strict';

  var cmds = document.getElementsByClassName('del');
  var i;

  for (i = 0; i < cmds.length; i++) {
    cmds[i].addEventListener('click', function(e) {
      e.preventDefault();
      if (confirm('are you sure?')) {
        document.getElementById('form_' + this.dataset.id).submit();
      }
    });
  }

})();
@section('content')
<h1>
	<a href="{{ url('/articles/create')}}" class="register">新規登録</a>
	Articles
</h1>
	<h3>氏名一覧</h3>
	<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> 
		<a href="#" class="del" data-id="{{ $article->id }}">[x]</a>
		<form method="post" action="{{ url('/articles', $article->id) }}" id="form_{{ $article->id }}">
      {{ csrf_field() }}
      {{ method_field('delete') }}
    </form>
		</li>
		@endforeach
	</ul>
	<script src="/js/main.js"></script>
@endsection

勘弁してよね。

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!