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
プロダクトコードの複雑さを計測せよ〜5分ではじめるPhpMetrics活用リファクタリング〜
kotomin_m
0
210
私が新卒からプロへと変わる3年間~「エンジニア基礎」研修資料で伝えたエンジニアになるまでの道のり~
kotomin_m
9
4.6k
わたしと技術コミュニティとキャリア
kotomin_m
2
1.3k
【技術カンファレンス運営の裏側】Iwaken Lab 技術好き学生の近況報告 & ことみんさんに技術カンファレンス運営の裏側を聞いちゃう会
kotomin_m
4
1.3k
倒して、倒して、倒しまくれ!―PHP&Laravelのバージョンアップの戦い―
kotomin_m
4
1.5k
新米SRE、4つのプロダクトを同時にPHP7.0から8.1へ!!
kotomin_m
2
2.5k
偶然のチャンスを掴めるのは誰だ?
kotomin_m
11
2k
エンジニア基礎 ウィルゲート2024年度エンジニア新卒研修
kotomin_m
171
200k
チーム開発でデプロイ頻度を上げるための設計とタスク分割
kotomin_m
5
8k
Other Decks in Programming
See All in Programming
Linux && Docker 研修/Linux && Docker training
forrep
24
4.5k
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
150
もう僕は OpenAPI を書きたくない
sgash708
5
1.8k
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
160
PHP ステートレス VS ステートフル 状態管理と並行性 / php-stateless-stateful
ytake
0
100
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
570
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
570
Unity Android XR入門
sakutama_11
0
160
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
120
CI改善もDatadogとともに
taumu
0
120
Featured
See All Featured
Navigating Team Friction
lara
183
15k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
BBQ
matthewcrist
87
9.5k
A Philosophy of Restraint
colly
203
16k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
960
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
550
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
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