データベースとの連携(1) DBクラス
Laravelからデータベースへのアクセスは複数の方法がありますが、最初は先ず、ネイティブPHPをから使っているSQL色の強いDBクラスについて説明します。
DBクラスのクエリはSQLをそのまま使うので、過去に「mysqli」や「PDO」を使ったデータベース操作経験がある方はすぐ理解できると思います。
データの準備
データベースが未作成の場合は、新規で作成しましょう。
テーブル名:members
フィールド:id(連番)、name(氏名)、age(年齢)mail(メールアドレス)
テーブルを作成
1 2 3 4 5 6 7 |
CREATE TABLE members( id integer auto_increment, name varchar(16) not null, age tinyint not null, email varchar(256) not null, primary key (id) ); |
ダミーデータの準備
1 2 3 4 5 6 7 |
insert into members(name,age,email) values ( 'yamada' , 31 , 'yamada@cyber-genjin.net' ), ( 'tanaka' , 21 , 'tanaka@cyber-genjin.net' ), ( 'sato' , 36 , 'sato@cyber-genjin.net' ), ( 'harada' , 47 , 'harada@cyber-genjin.net' ), ( 'saito' , 33 , 'saito@cyber-genjin.net' ), ( 'ikeda' , 50 , 'ikeda@cyber-genjin.net' ); |
ルーティングの準備
1 2 3 4 5 |
Route::get('/select', [App\Http\Controllers\MemberController::class, 'select']); Route::get('/select2', [App\Http\Controllers\MemberController::class, 'select2']); Route::get('/insert', [App\Http\Controllers\MemberController::class, 'insert']); Route::get('/update', [App\Http\Controllers\MemberController::class, 'update']); Route::get('/delete', [App\Http\Controllers\MemberController::class, 'delete']); |
データの抽出 SELECT
DB::select( SQLクエリ ,パラメータ配列 );
最初は、第1引数のクエリのみで実行してみましょう。
MemberController
1 2 3 4 5 |
public function select() { $rows = DB::select('select * from members'); return view('index' , compact('rows')); } |
DBクラスのレコード出力
/resorces/views/index.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>DBクラスのレコード出力</title> </head> <body> <h1>DBクラスのレコード出力</h1> <table> <tr> <th>登録番号</th> <th>氏名</th> <th>年齢</th> <th>メール</th> </tr> @foreach( $rows as $row ) <tr> <td>{{ $row->id }}</td> <td>{{ $row->name }}</td> <td>{{ $row->age }}</td> <td>{{ $row->email }}</td> </tr> @endforeach </table> </body> </html> |
ではクエリに抽出条件を指定した場合は、
1 |
$rows = DB::select('select * from members where age=21'); |
このage(年齢)の部分は、ユーザーが条件を入力して検索する場合は、意図しないSQLが実行されるリスクが残ります。
PDOやMysqliなどのドライバを使用した場合はプリペアドステートメントで処理します。
LaravelのDBクラスにも同様の機能が用意されてます。
今回の場合は、ageにプレースホルダを設定してパラメータで紐付けます。
1 2 3 4 5 6 |
public function select2() { $parametar = ['age' => '21']; $rows = DB::select('select * from members where age=:age', $parameter); return view('index' , compact('rows')); } |
こうすることでSQLインジェクション対策になります。
検索条件に値を指定する場合はこの形になります。
DBクラスで動的な値を設定する場合は、第2引数に必ずパラーメータを設定しましょう。
・クエリの値にプレースホルダを使って設定
・パラメータでプレースホルダと値の紐付け
・クエリを実行
データの挿入 INSERT
DB::insert( SQLクエリ , パラメータ配列 )
以下のレコードをテーブルに挿入(追加)するソースです。
1 2 3 4 5 6 7 8 9 10 |
public function insert() { $parametar = [ 'name' => 'takahashi', 'age' => '21', 'email' => 'takahashi@cyber-genjin' ]; DB::insert( 'insert into members ( nama, age, email ) values ( :name, :age, :email ) ', $parameter) ); return redirect('index'); } |
データの更新 UPDATE
DB::update( SQLクエリ, パラメータ配列 );
id の値4のレコードのemailを更新します。
1 2 3 4 5 6 7 8 9 10 |
public function update() { $parametar = [ 'id' => '4', 'email' => 'takahashi-san@cyber-genjin' ]; DB::update( 'uodate members set email=:email where id=:id' , $parameter ); return redirect('index'); } |
データの削除 DELETE
DB::delete( SQLクエリ , パラメータ配列 );
id の値4のレコードを削除します。
1 2 3 4 5 6 7 |
public function delete() { $parametar = [ 'id' => '4',]; DB::delete( 'delete from members where id=:id' , $parameter ); return redirect('index'); } |