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
370
私が新卒からプロへと変わる3年間~「エンジニア基礎」研修資料で伝えたエンジニアになるまでの道のり~
kotomin_m
10
4.9k
わたしと技術コミュニティとキャリア
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
color-scheme: light dark; を完全に理解する
uhyo
6
460
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
940
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
580
CSS Linter による Baseline サポートの仕組み
ryo_manba
1
130
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
190
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
130
Formの複雑さに立ち向かう
bmthd
1
880
時計仕掛けのCompose
mkeeda
1
310
Djangoにおける複数ユーザー種別認証の設計アプローチ@DjangoCongress JP 2025
delhi09
PRO
4
380
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
780
Pulsar2 を雰囲気で使ってみよう
anoken
0
240
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
3
1.7k
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
350
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Optimizing for Happiness
mojombo
376
70k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
RailsConf 2023
tenderlove
29
1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Documentation Writing (for coders)
carmenintech
67
4.6k
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