最終更新日:2019‐10-22
テーブルの作成とDatabaseの設定
データベースにテーブルを作成します。今回はジョジョのスタンド一覧を表示したいと思います。
drop table stands; CREATE TABLEstands
(id
MEDIUMINT NOT NULL AUTO_INCREMENT,name
varchar(36) NOT NULL COMMENT 'スタンド名',owner
varchar(36) NOT NULL COMMENT 'スタンド使い', PRIMARY KEY (id
) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='stands'
DBの動いているポートを確認します。今回はXAMPPでMySQL(MariaDB)を使用しています。

Laravelで作ったプロジェクトのDBの設定を確認します。設定ファイルの場所は以下です。
[プロジェクト名]\config\database.php

中を見ると、いろいろなデータベースの設定が記述されていますが、今回はmysqlを確認しましょう。意図して変更していなければデフォルトのままでOKなはず。

次にチェックしておきたいのが、環境ファイルの「.env」です。環境ごとに設定を変更できます。
Illuminate\Database\QueryExceptionSQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: ・・・)
とエラーが表示されてしまったら、まずこの.envファイルを見直しましょう。データベースがデフォルトのままだとまずSQL実行でエラーになってしまうので、MySQLに自分で作成したデータベース名と、ユーザー名/パスワードを設定してあげましょう。

Controllerクラスの作成
次にコントローラーを作成します。artisanコマンドでコントローラーを作成します。
php artisan make:controller [コントローラー名]


自動生成されたコントローラークラスの続きを書いていきます。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class StandListController extends Controller
{
function search(Request $request){
$stands = DB::select('select * from stands order by id');
return view('stands.search',['stands' => $stands]);
}
}
Viewの作成
次にViewも作ります。standsフォルダを作成して、search.blade.phpを作成しました。

C#のRazorをやったことがある方はハマりポイントがあって、Foreach文の配列から要素を代入する部分の書き方が逆になります。(C#は foreach(var item in items)の書き方)
<head>
<title>jojo stands list</title>
<style>
table {
border-collapse: collapse;
margin: 0 auto;
padding: 0;
width: auto;
}
table tr {
background-color: #fff;
border-bottom: 2px solid #fff;
}
table tr:nth-child(even){
background-color: #E1FDFE;
}
table th,
table td {
padding: .35em 1em;
}
table thead th {
font-size: .85em;
padding: 1em;
}
table thead tr{
background-color: #fd6767;
color:#fff;
}
table tbody th {
text-align: left;
font-size: .8em;
}
</style>
</head>
<body>
<div class="searchBox">
<form name="search" action="/stands/">
<input type="text" name="keyword" maxlength="100">
<input type="submit">
</form>
</div>
<table>
@foreach($stands as $stand)
<tr>
<td>{{$stand->id}}</td>
<td>{{$stand->name}}</td>
<td>{{$stand->owner}}</td>
</tr>
@endforeach
</table>
</body>
ルーティングの追加
最後にRouteにも該当のパスを通します。(※Routeっていちいち書かなきゃ駄目なんですかね…?)
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can registerStandListController web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('sigh','\App\Http\Controllers\JotaroController@sigh');
Route::get('stands','\App\Http\Controllers\StandListController@search');
コードは完成です。実行するとこんな感じになります。

技術の進化についていけていないシステムエンジニア。浅く広く、何でも大体はこなせるエンジニアで重宝されてる(つもり)。でもやっぱり特化した武器欲しい。
備忘録として記事を投稿していますが、少しでも誰かの助けになればと思います。
最近はプロジェクトでReact(TypeScript )を触っています。
趣味でギター弾きます。