Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Eloquentとクエリビルダを両方使った実装の失敗例 - 第150回 PHP勉強会 #php...
Search
ことみん / kotomin_m
March 15, 2023
Programming
1
1k
Eloquentとクエリビルダを両方使った実装の失敗例 - 第150回 PHP勉強会 #phpstudy
ことみん / kotomin_m
March 15, 2023
Tweet
Share
More Decks by ことみん / kotomin_m
See All by ことみん / kotomin_m
わたしと技術コミュニティとキャリア
kotomin_m
2
1.1k
【技術カンファレンス運営の裏側】Iwaken Lab 技術好き学生の近況報告 & ことみんさんに技術カンファレンス運営の裏側を聞いちゃう会
kotomin_m
4
1.2k
倒して、倒して、倒しまくれ!―PHP&Laravelのバージョンアップの戦い―
kotomin_m
4
1.4k
新米SRE、4つのプロダクトを同時にPHP7.0から8.1へ!!
kotomin_m
2
2.4k
偶然のチャンスを掴めるのは誰だ?
kotomin_m
11
1.9k
エンジニア基礎 ウィルゲート2024年度エンジニア新卒研修
kotomin_m
169
190k
チーム開発でデプロイ頻度を上げるための設計とタスク分割
kotomin_m
5
7.9k
ISUCONってなんだか難しそう……!!でも、初めてのISUCONにPHPで挑戦してきました!
kotomin_m
2
2.3k
PHPコミュニティ、その魅力と熱狂をあなたにも!!!
kotomin_m
1
2.5k
Other Decks in Programming
See All in Programming
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
0
220
Linux && Docker 研修/Linux && Docker training
forrep
22
4.1k
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2k
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
450
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1.2k
AWSマネコンに複数のアカウントで入れるようになりました
yuhta28
2
150
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
270
[Fin-JAWS 第38回 ~re:Invent 2024 金融re:Cap~]FaultInjectionServiceアップデート@pre:Invent2024
shintaro_fukatsu
0
340
ASP.NET Core の OpenAPIサポート
h455h1
0
160
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
250
chibiccをCILに移植した結果 (NGK2025S版)
kekyo
PRO
0
190
Featured
See All Featured
Done Done
chrislema
182
16k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
We Have a Design System, Now What?
morganepeng
51
7.4k
How GitHub (no longer) Works
holman
312
140k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
Scaling GitHub
holman
459
140k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Navigating Team Friction
lara
183
15k
Code Review Best Practice
trishagee
65
17k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
192
16k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Transcript
@kotomin_m #phpstudy Eloquentとクエリビルダを 両方使った実装の失敗例 ことみん 第150回 PHP勉強会@東京 @kotomin_m #phpstudy
@kotomin_m #phpstudy ことみん @kotomin_m 所属: 株式会社ウィルゲート 21卒 趣味: LT会とカンファレンス 特技:
懇親会で仲良くなる
@kotomin_m #phpstudy 1. 用語を知ろう 2. 実装の失敗例 Eloquentとクエリビルダを 両方使った実装の失敗例
@kotomin_m #phpstudy 用語を知ろう • Eloquent • アクセサ • ミューテタ •
クエリビルダ
@kotomin_m #phpstudy Eloquent
@kotomin_m #phpstudy Eloquent • データベース操作を楽しくする、オブジェクトリ レーショナルマッパー(ORM) • 各データベーステーブルに対応する「モデル」 を使用 •
レコードの取得、挿入、更新、削除が可能 https://readouble.com/laravel/10.x/ja/eloquent.html
@kotomin_m #phpstudy アクセサ
@kotomin_m #phpstudy アクセサ Eloquentの属性値にアクセスがあった時に、その 値を変換するもの https://readouble.com/laravel/10.x/ja/eloquent-mutators.html
@kotomin_m #phpstudy アクセサ(Laravel 5.6) アクセスしたいカラム名がFooの場合、 getFooAttributeメソッドをモデルに作成 https://readouble.com/laravel/5.6/ja/eloquent-mutators.html
@kotomin_m #phpstudy アクセサ(Laravel 5.6) class User extends Model { /**
* @param string $value * @return string */ public function getFirstNameAttribute($value) { // 復号する return decrypt($value); } }
@kotomin_m #phpstudy アクセサ(Laravel 10.x) • メソッド名はカラム名の「キャメルケース」で protectedなメソッドをモデルに作成 • 戻り値のタイプヒントは Illuminate\Database\Eloquent\Casts\Attribute
◦ Attributeクラスのコンストラクタにget引数を与えます。 https://readouble.com/laravel/10.x/ja/eloquent-mutators.html
@kotomin_m #phpstudy class User extends Model { /** * ユーザの名前取得
*/ protected function firstName(): Attribute { return Attribute::make( get: fn (string $value) => ucfirst($value), ); } } アクセサ(Laravel 10.x)
@kotomin_m #phpstudy ミューテタ
@kotomin_m #phpstudy ミューテタ Eloquentの属性値を設定するときに、その値を変 換するもの https://readouble.com/laravel/10.x/ja/eloquent-mutators.html
@kotomin_m #phpstudy ミューテタ(Laravel 5.6) アクセスしたいカラム名がFooの場合、 setFooAttributeメソッドをモデルに作成 https://readouble.com/laravel/5.6/ja/eloquent-mutators.html
@kotomin_m #phpstudy ミューテタ(Laravel 5.6) class User extends Model { /**
* @param string $value * @return void */ public function setFirstNameAttribute($value) { // 暗号化する $this->attributes['first_name'] = encrypt($value); } }
@kotomin_m #phpstudy ミューテタ(Laravel 10.x) 属性を定義するときに set という引数を指定する https://readouble.com/laravel/10.x/ja/eloquent-mutators.html
@kotomin_m #phpstudy class User extends Model { protected function firstName():
Attribute { return Attribute::make( get: fn (string $value) => ucfirst($value), set: fn (string $value) => strtolower($value), ); } } ミューテタ(Laravel 10.x)
@kotomin_m #phpstudy クエリビルダ
@kotomin_m #phpstudy クエリビルダ • クエリを作成し実行するために使用 • アプリケーションで行われるほとんどのデータ ベース操作が可能 ◦ select,
where …… https://readouble.com/laravel/5.6/ja/queries.html
@kotomin_m #phpstudy クエリビルダ $users = DB::table('users') ->select('name','email as user_email') ->get();
@kotomin_m #phpstudy Eloquentとクエリビルダ 組み合わせた実装の失敗例
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] );
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] );
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] );
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); getメソッドは、クエリの結果を含む Illuminate\Support\Collection インスタンスを返す
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); Eloquentで使用するモデル
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); 第一引数に指定したカラムのモデルが 存在していたら更新、なかったら追加す る
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); ユーザ履歴テーブル(user_history)の user_idと$users->idが一致するモデルが あれば更新、なかったら追加する
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] );
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); ミューテタが使われるので、暗号化されて 保存
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); クエリビルダで取得しているので、アク セサが使われない →複合されていない名前を取得してい る
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); すでに暗号化された状態のデータを さらに暗号化して保存
@kotomin_m #phpstudy // クエリビルダでデータベースから値を取得 $users = DB::table('users') ->select('id','first_name', 'last_name') ->get();
// EloquentのupdateOrCreate()を利用してDBを更新 $this->userHistory->updateOrCreate( ['user_id' => $users->id], [ 'last_name' => $users->last_name, 'first_name' => $users->first_name, ] ); 失敗→二重で暗号化されてデータ ベースに保存されていた
@kotomin_m #phpstudy Eloquentとクエリビルダを 両方使った実装の失敗例 ことみん 第150回 PHP勉強会@東京 @kotomin_m #phpstudy