この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:17
ページ更新者:guest
更新日時:2026-05-15 05:24:41

タイトル: クイックスタート & チュートリアル(初心者向け)

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<p>当記事では<strong>Laravel</strong>を使用して<strong>Webアプリケーションを作成</strong>します。</p>

<p>画面から値の入力、取得、削除をする簡単なアプリです。</p>

<p>アプリの作成を通してLaravelの以下の項目を説明します。</p>

<p>・データベース<br />
・Eloqunetモデル ORM<br />
・ルーティング<br />
・バリデーション<br />
・ビュー<br />
・Bladeテンプレート</p>

<p>※Laravelの導入が済んでいない場合は<a href="https://jpn.itlibra.com/article?id=10857">こちら</a>を参照。</p>

<p>※Windowsクライアントを使用する方はXAMPPを使用するのでXAMPPの導入が済んでいない場合は<a href="https://jpn.itlibra.com/article?id=10339">こちら</a>を参照。</p>

<p>&nbsp;</p>

<h2>インストール</h2>

<p>「<strong><span style="color:#000000">quickstart</span></strong>」という名前のプロジェクトを新規に作成しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">composer </span><span style="color:#f1c40f">create-project</span><span style="color:#ffffff"> laravel/laravel quickstart --prefer-dist</span></td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>データベースの準備</h2>

<p><strong>■データベースの作成(Windowsユーザーの場合)</strong></p>

<p><strong>XAMPP</strong>のControl Panelから<strong>phpMyAdmin</strong>を起動しましょう。</p>

<p><img alt="" src="/uploadImage/20180322003236_1796658.png" /></p>

<p><strong>phpMyAdmin</strong>を開いたら「<strong>New</strong>」ボタンを押して「<strong>test</strong>」データーベースを作成しましょう。</p>

<p>&nbsp;</p>

<p><strong>■データベースの作成(Linux / Mac)</strong></p>

<p>ターミナルで以下のコマンドを実行します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">sudo get mysql-server</span></p>

			<p><span style="color:#ffffff">sudo get php-mysqlnd</span></p>

			<p><span style="color:#ffffff">sudo mysql -u root</span></p>

			<p><span style="color:#ffffff">create database test;</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p><strong>■ユーザーの作成</strong></p>

<p>「test」データベースを選択した状態で以下のコマンドを実行してデーターベースユーザーを作成しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">GRANT ALL PRIVILEGES&nbsp;<br />
			ON test.*&nbsp;<br />
			TO testUser@localhost&nbsp;<br />
			IDENTIFIED BY 'testPass';</span></td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p><strong>■データーベースの接続設定</strong></p>

<p>Laravel内にあるデータベースの接続設定を変更します。</p>

<p>ルートフォルダ直下の「.env」ファイルの以下の部分を変更しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">DB_CONNECTION=mysql<br />
			DB_HOST=127.0.0.1<br />
			DB_PORT=3306<br />
			DB_DATABASE=test<br />
			DB_USERNAME=testUser<br />
			DB_PASSWORD=testPass</span></td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p><strong>■テーブルの作成</strong></p>

<p>データベースのマイグレーション機能を用いて「<strong>task</strong>」テーブルを作成してみましょう。</p>

<p>ルートディレクトリに移動して以下のコマンドを実行します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">php artisan </span><span style="color:#f1c40f">make:migration</span><span style="color:#ffffff"> create_tasks_table --create=tasks</span></td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>「<strong>database\migrations</strong>」配下にファイルが新規作成されたことを確認できます。</p>

<p>ファイルの中身を見てみましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">public function up()<br />
			&nbsp; &nbsp; {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; Schema::create('tasks', function (Blueprint $table) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->increments('id');<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->timestamps();<br />
			&nbsp; &nbsp; &nbsp; &nbsp; });<br />
			&nbsp; &nbsp; }</span></td>
		</tr>
	</tbody>
</table>

<p>この関数内にある'id'とはtasksテーブルのカラムです。</p>

<p>&nbsp;</p>

<p>tasksテーブルにカラムを追加してみましょう。</p>

<p>以下の例ではstring型のnameというカラムを追加しています。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">public function up()<br />
			&nbsp; &nbsp; {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; Schema::create('tasks', function (Blueprint $table) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->increments('id');</span><br />
			<span style="color:#f1c40f">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->string('name');</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->timestamps();<br />
			&nbsp; &nbsp; &nbsp; &nbsp; });<br />
			&nbsp; &nbsp; }</span></td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>ファイルを書き換えただけではテーブルの構造は変化しません。</p>

<p>ファイルを保存して以下のコマンドを実行しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">php artisan migrate</span></td>
		</tr>
	</tbody>
</table>

<p>※デフォルトのテーブルでkey関連のエラーが発生した場合は「<strong>database\migrations</strong>」配下にある該当テーブルファイルの「->unique()」や「->index()」の記述を削除するか、対象のカラムの文字コードを以下のようにUTF-8に変更しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">$table->string('email')->charset('utf8')->unique();</span></td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>Eloqunetモデル ORM</h2>

<p>次にDBからデータの保存、取得をする際に利用する<strong>Eloqunet</strong>というORMを作成しましょう。</p>

<blockquote>
<p>※「ORM(O/R Mapping)」とは</p>

<p>O/Rは「Object/Relational」の略で、オブジェクトとリレーショナルデータベースのデータの間をマッピングする技術のこと。</p>
</blockquote>

<p>&nbsp;</p>

<p>以下のコマンドを実行して「<strong>Task</strong>」というモデルを作成します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">php artisan </span><span style="color:#f1c40f">make:model </span><span style="color:#ffffff">Task</span></td>
		</tr>
	</tbody>
</table>

<p>モデルとなるクラスは、指定した名前でプロジェクトの<strong>「app」フォルダ内</strong>に作成されるので確認しましょう。</p>

<p>&nbsp;</p>

<p><strong>【Task.php】の中身</strong></p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff"><?php</span>

			<p><span style="color:#ffffff">namespace App;</span></p>

			<p><span style="color:#ffffff">use Illuminate\Database\Eloquent\Model;</span></p>

			<p><span style="color:#ffffff">class Task </span><span style="color:#f1c40f">extends Model</span><br />
			<span style="color:#ffffff">{<br />
			&nbsp; &nbsp; //<br />
			}</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>&nbsp;</p>

<h2>ルーティング</h2>

<p><strong>ルーティング</strong>とはコントローラとURLを対応させる仕組みです。</p>

<p>※コントローラーの詳細に関しては当記事では触れません。(中級者向けの記事で説明します)</p>

<p>&nbsp;</p>

<p>「<strong>app/Http/routes.php</strong>」のファイルにルーティングの定義をします。</p>

<p><span style="color:#e74c3c">※Laravel5.3以降は、「<strong>routes</strong>」フォルダ配下にある「<strong>web.php</strong>」がそれに該当します。</span></p>

<p><span style="color:#000000">とりあえず、以下の定義をしておきましょう。</span></p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff"><?php</span>

			<p><span style="color:#ffffff">use App\Task;<br />
			use Illuminate\Http\Request;</span></p>

			<p><span style="color:#ffffff">Route::get('/', function () {</span><br />
			<span style="color:#f1c40f">&nbsp; &nbsp; return view('tasks');</span><br />
			<span style="color:#ffffff">});</span></p>

			<p><span style="color:#ffffff">Route::post('/task', function (Request $request) {<br />
			&nbsp; &nbsp; //<br />
			});</span></p>

			<p><span style="color:#ffffff">Route::delete('/task/{task}', function (Task $task) {<br />
			&nbsp; &nbsp; //<br />
			});</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>&nbsp;</p>

<h2>ビューについて</h2>

<p>Laravelのビューは「<strong>resources/views</strong>」に保管します。</p>

<p>「<span style="color:#e74c3c"><strong>return view('tasks');</strong></span>」と定義したのは「<strong>resources/views/tasks.blade.php</strong>」を表示するということです。</p>

<p>それでは実際に「<strong>tasks.blade.php</strong>」をつくりましょう。</p>

<p>&darr;が今回作るビューの完成形です。(※今回CSSはいじらないのであくまでイメージです)</p>

<p><img alt="" src="/uploadImage/20180322095921_4834714.png" /></p>

<p>&nbsp;</p>

<h2>レイアウトの作成</h2>

<p>Laravelは<span style="color:#e74c3c"><strong>Bladeテンプレート</strong></span>を使用することでページ間の特徴を共有できます。</p>

<p>今回はレイアウトを「<strong>resources/views/layouts/app.blade.php</strong>」として定義します。</p>

<p>「<strong>.blade.php</strong>」拡張子とすることで<strong>Bladeテンプレートエンジン</strong>をするとフレームワークに指示を出します。</p>

<p>それでは「<strong>resources/views</strong>」に「layouts」フォルダを作成して以下の内容の「<strong>app.blade.php</strong>」を作成しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff"><!-- resources/views/layouts/app.blade.php --></span>

			<p><span style="color:#ffffff"><!DOCTYPE html><br />
			<html lang="en"><br />
			&nbsp; &nbsp; <head><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <title>Laravel Quickstart - Basic</title></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <!-- CSSとJavaScript --><br />
			&nbsp; &nbsp; </head></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <body><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <div class="container"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <nav class="navbar navbar-default"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- ナビバーの内容 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </nav><br />
			&nbsp; &nbsp; &nbsp; &nbsp; </div></span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; &nbsp; &nbsp; @yield('content')</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; </body><br />
			</html></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>「<span style="color:#e74c3c"><strong>@yield('content')</strong></span>」は「<strong>contentセクション</strong>」を持つ子ビューを挿入するという意味です。</p>

<p>&nbsp;</p>

<h2>子ビューの作成</h2>

<p>それでは「<strong>contentセクション</strong>」を持つ子ビューを作成しましょう。</p>

<p>※先ほど作ったのは<strong>レイアウト</strong>で今回は<strong>ビュー</strong>です。</p>

<p>「<strong>resources/views/tasks.blade.php</strong>」としてビューを定義します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff"><!-- resources/views/tasks.blade.php --></span>

			<p><span style="color:#f1c40f">@extends('layouts.app')</span></p>

			<p><span style="color:#f1c40f">@section('content')</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <!-- Bootstrapの定形コード&hellip; --></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <div class="panel-body"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <!-- バリデーションエラーの表示 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; @include('common.errors')</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <!-- 新タスクフォーム --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <form action="{{ url('task') }}" method="POST" class="form-horizontal"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ csrf_field() }}</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- タスク名 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="task" class="col-sm-3 control-label">タスク</label></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-6"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name" id="task-name" class="form-control"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- タスク追加ボタン --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-offset-3 col-sm-6"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-default"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-plus"></i> タスク追加<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; </form><br />
			&nbsp; &nbsp; </div></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <!-- TODO: 現在のタスク --></span><br />
			<span style="color:#f1c40f">@endsection</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>「<strong>@extends('layouts.app')</strong>」は先ほど作成した「<strong>resources/views/layouts/app.blade.php</strong>」レイアウトを使用するという意味です。</p>

<p>「<span style="color:#000000"><strong>@section('content')</strong></span>」から「<span style="color:#000000"><strong>@endsection</strong></span>」の内容を、先ほどのレイアウトで定義した「<span style="color:#e74c3c"><strong>@yield('content')</strong></span>」の部分に挿入します。</p>

<p>「<strong>@include('common.errors')</strong>」は「<strong>resources/views/common/errors.blade.php</strong>」のテンプレートを使用するという意味です。</p>

<p>(このテンプレートは後ほど作成します。)</p>

<p>&nbsp;</p>

<p>画面を表示させてみましょう。</p>

<p>ルートディレクトリ上で以下のコマンドを実行してサーバーを起動します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">php artisan </span><span style="color:#f1c40f">serve</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>とりあえず表示させるには「<strong><span style="color:#000000">@include('common.errors')</span></strong>」を一時的に削除して「<strong>http://localhost:8000/</strong>」にアクセスしてみましょう。</p>

<p>以下の画面が表示されます。※表示したら「<strong><span style="color:#000000">@include('common.errors')</span></strong>」は戻しておいてください。</p>

<p><img alt="" src="/uploadImage/20180322120252_5928471.png" /></p>

<p>&nbsp;</p>

<h2>バリデーション(入力値チェック)</h2>

<p>「<strong>tasks</strong>」テーブルに追加する値のチェックを行います。</p>

<p>値の追加はPOSTメソッドで行うので「<strong>app/Http/routes.php</strong>」もしくは「<strong>routes/web.php</strong>」に以下の記述をしましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">Route::post('/task', function (Request $request) {</span>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; $validator = Validator::make($request->all(), [<br />
			&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'required|max:255',<br />
			&nbsp; &nbsp; ]);</span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; if ($validator->fails()) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; return redirect('/')<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withInput()<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withErrors($validator);<br />
			&nbsp; &nbsp; }</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; // タスク作成処理&hellip;<br />
			});</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>これは「<strong>name</strong>」フィールドの入力値に<span style="color:#2980b9"><strong>何かしらの値が入力</strong></span>されていて、かつ<span style="color:#2980b9"><strong>255字以内</strong></span>であることをチェックしています。</p>

<p>もし、入力値チェックにひっかかった場合は<strong><span style="color:#000000">「'/'」</span></strong><span style="color:#000000">にリダイレクトします。</span></p>

<p>「<strong><span style="color:#000000">->withInput()</span></strong>」で入力値を再表示し、「<span style="color:#000000"><strong>->withErrors($validator)</strong></span>」でエラーを<strong>$errors</strong>変数に格納します。</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<h2>エラーの表示</h2>

<p>次は<strong>$errors</strong>変数は子ビューの「<strong>@include('common.errors')</strong>」の部分で表示させるようにしましょう。</p>

<p>「<strong>resources/views/common/errors.blade.php</strong>」を作成して以下の実装をしましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff"><!-- resources/views/common/errors.blade.php --></span>

			<p><span style="color:#ffffff">@if (count($errors) > 0)<br />
			&nbsp; &nbsp; <!-- Form Error List --><br />
			&nbsp; &nbsp; <div class="alert alert-danger"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <strong>入力値に誤りがあります。</strong></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <br><br></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <ul><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($errors->all() as $error)<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>{{ $error }}</li><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach<br />
			&nbsp; &nbsp; &nbsp; &nbsp; </ul><br />
			&nbsp; &nbsp; </div><br />
			@endif</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>データベースへの値の追加</h2>

<p>次は値をデータベースに追加する処理を記載します。</p>

<p>「<strong>app/Http/routes.php</strong>」もしくは「<strong>routes/web.php</strong>」に記載したバリデーション続きに以下の内容を追加しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">Route::post('/task', function (Request $request) {</span>

			<p><span style="color:#ffffff">&nbsp; &nbsp; $validator = Validator::make($request->all(), [<br />
			&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'required|max:255',<br />
			&nbsp; &nbsp; ]);</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; if ($validator->fails()) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; return redirect('/')<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withInput()<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withErrors($validator);<br />
			&nbsp; &nbsp; }</span></p>

			<p><strong><span style="color:#ffffff">&nbsp;</span></strong><span style="color:#f1c40f"> &nbsp; $task = <strong>new Task</strong>;<br />
			&nbsp; &nbsp; $task->name = $request->name;<br />
			&nbsp; &nbsp; $task->save();</span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; return redirect('/');</span><br />
			<span style="color:#ffffff">});</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>「<strong>new Task</strong>」でモデルを生成して「<strong>name</strong>」に値をセット、「<strong>save()</strong>」で保存をします。</p>

<p>&nbsp;</p>

<h2>データベースから値を取得</h2>

<p>保存した値を取得する処理を以下のように実装します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">Route::get('/', function () {</span>

			<p><span style="color:#ffffff">&nbsp; &nbsp; $tasks = Task::orderBy('created_at', 'asc')-><strong>get()</strong>;</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; return view('</span><span style="color:#f1c40f">tasks</span><span style="color:#ffffff">', [<br />
			&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f">'tasks' => $tasks</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; ]);<br />
			});</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>「<span style="color:#000000"><strong>get()</strong></span>」で値を取得します。「<span style="color:#000000"><strong>orderBy('created_at', 'asc')</strong></span>」は取得順序の指定です。</p>

<p>「<span style="color:#e74c3c"><strong>tasks.blade.php</strong></span>」に対して取得した「<span style="color:#000000"><strong>$tasks</strong>」を渡します。</span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>ビューに値を表示</p>

<p>データベースから取得した値をビューに表示しましょう。</p>

<p>「<strong>resources/views/tasks.blade.php</strong>」を以下の内容に書き換えましょう。</p>

<p>(※黄色が追加した部分です)</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000"><span style="color:#ffffff">@extends('layouts.app')</span>

			<p><span style="color:#ffffff">@section('content')</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <!-- Bootstrapの定形コード&hellip; --></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <div class="panel-body"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <!-- バリデーションエラーの表示 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; @include('common.errors')</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <!-- 新タスクフォーム --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <form action="{{ url('task') }}" method="POST" class="form-horizontal"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ csrf_field() }}</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- タスク名 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="task" class="col-sm-3 control-label">タスク</label></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-6"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name" id="task-name" class="form-control"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- タスク追加ボタン --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-offset-3 col-sm-6"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-default"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-plus"></i> タスク追加<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; </form><br />
			&nbsp; &nbsp; </div></span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; <!-- 現在のタスク --><br />
			&nbsp; &nbsp; @if (count($tasks) > 0)<br />
			&nbsp; &nbsp; &nbsp; &nbsp; <div class="panel panel-default"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="panel-heading"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 現在のタスク<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div></span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="panel-body"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table class="table table-striped task-table"></span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- テーブルヘッダ --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Task</th><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp;</th><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead></span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- テーブル本体 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($tasks as $task)<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- タスク名 --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="table-text"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>{{ $task->name }}</div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td></span></p>

			<p><span style="color:#f1c40f">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- TODO: 削除ボタン --><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table><br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; &nbsp; &nbsp; </div><br />
			&nbsp; &nbsp; @endif</span><br />
			<span style="color:#ffffff">@endsection</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>「<strong><span style="color:#000000">$tasks</span></strong>」が0件でない場合、foreach文で回して一件ずつ「<strong>name</strong>」を表示します。</p>

<p>&nbsp;</p>

<p>データベースの値の削除</p>

<p>先ほど追加したコードの「<strong><span style="color:#f1c40f"><!-- TODO: 削除ボタン --></span></strong>」の部分に、以下のコードを追加して削除ボタンを追加しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><form action="{{ </span><span style="color:#f1c40f">url('task/'.$task->id)</span><span style="color:#ffffff"> }}" method="</span><span style="color:#f1c40f">POST</span><span style="color:#ffffff">"><br />
			&nbsp; &nbsp; {{ csrf_field() }}<br />
			&nbsp; &nbsp; {{ method_field('</span><span style="color:#f1c40f">DELETE</span><span style="color:#ffffff">') }}</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <button type="submit" class="btn btn-danger"><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-trash"></i> 削除<br />
			&nbsp; &nbsp; </button><br />
			</form></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>削除ボタンをクリックすると、<span style="color:#e74c3c"><strong>DELETE</strong></span>でリクエストします。</p>

<p>formタグ内では<span style="color:#e74c3c"><strong>POST</strong></span>を使用していますが、これはHTMLのフォームが許容しているのがGETとPOSTのみなので、とりあえず記載しているだけです。</p>

<p>実際はDELETEのリクエストとなります。</p>

<p>&nbsp;</p>

<p>「<strong>app/Http/routes.php</strong>」もしくは「<strong>routes/web.php</strong>」に以下のDELETEの処理を実装しましょう。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">Route::</span><span style="color:#f1c40f">delete</span><span style="color:#ffffff">('/task/{task}', function (Task $task) {<br />
			&nbsp; &nbsp; $task-></span><span style="color:#f1c40f">delete()</span><span style="color:#ffffff">;</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; return redirect('/');<br />
			});</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>これで今回のWebアプリ作成は完了となります。</p>

<p>お疲れさまでした。</p>

<p>&nbsp;</p>