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勉強会 #phpstudy
Search
ことみん / kotomin_m
March 15, 2023
Programming
1
690
Eloquentとクエリビルダを両方使った実装の失敗例 - 第150回 PHP勉強会 #phpstudy
ことみん / kotomin_m
March 15, 2023
Tweet
Share
More Decks by ことみん / kotomin_m
See All by ことみん / kotomin_m
偶然のチャンスを掴めるのは誰だ?
kotomin_m
10
570
エンジニア基礎 ウィルゲート2024年度エンジニア新卒研修
kotomin_m
137
140k
チーム開発でデプロイ頻度を上げるための設計とタスク分割
kotomin_m
5
5.9k
ISUCONってなんだか難しそう……!!でも、初めてのISUCONにPHPで挑戦してきました!
kotomin_m
2
820
PHPコミュニティ、その魅力と熱狂をあなたにも!!!
kotomin_m
1
1.4k
エラー発生時に冷静に対処している 先輩は何を考えているのか
kotomin_m
0
500
チームで継続する 週末のリファクタリング時間
kotomin_m
0
360
組織文化づくりのためにイベント開催で工夫していること_-_ファシリテートを学ぼう_コミュニティ達人の工夫.pdf
kotomin_m
0
400
はじめてのPHPコマンドラインオプション〜PHP開発を今よりちょっと楽しくする〜
kotomin_m
2
1.2k
Other Decks in Programming
See All in Programming
Elm Form Validation
bkuhlmann
0
520
Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | AndroidMakers Paris
prof18
0
170
使ってみよう Azure AI Document Intelligence
kosmosebi
2
370
Folding Cheat Sheet #4
philipschwarz
PRO
0
100
大規模Reactアプリのリアーキテクチャ~8万行のTanStack Query移行の軌跡~
kj455
4
1k
JavaScript Closure
asoluka
0
950
“Seeing Like a Programmer”—Resiliency, Limits, and Moral Hazards in Software Engineering (LambdaConf 2024)
chriskrycho
0
290
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
1.1k
Site Reliability Engineering for GMO
pyama86
9
1.1k
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
1
1.4k
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
930
禅の心を手に入れよ
eltociear
1
420
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
176
21k
Fireside Chat
paigeccino
22
2.6k
Infographics Made Easy
chrislema
238
18k
Scaling GitHub
holman
457
140k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
323
20k
Creatively Recalculating Your Daily Design Routine
revolveconf
211
11k
Done Done
chrislema
178
15k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
Being A Developer After 40
akosma
67
580k
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
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