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
650
Eloquentとクエリビルダを両方使った実装の失敗例 - 第150回 PHP勉強会 #phpstudy
ことみん / kotomin_m
March 15, 2023
Tweet
Share
More Decks by ことみん / kotomin_m
See All by ことみん / kotomin_m
エンジニア基礎 ウィルゲート2024年度エンジニア新卒研修
kotomin_m
131
140k
チーム開発でデプロイ頻度を上げるための設計とタスク分割
kotomin_m
5
5.7k
ISUCONってなんだか難しそう……!!でも、初めてのISUCONにPHPで挑戦してきました!
kotomin_m
2
730
PHPコミュニティ、その魅力と熱狂をあなたにも!!!
kotomin_m
1
1.3k
エラー発生時に冷静に対処している 先輩は何を考えているのか
kotomin_m
0
480
チームで継続する 週末のリファクタリング時間
kotomin_m
0
350
組織文化づくりのためにイベント開催で工夫していること_-_ファシリテートを学ぼう_コミュニティ達人の工夫.pdf
kotomin_m
0
400
はじめてのPHPコマンドラインオプション〜PHP開発を今よりちょっと楽しくする〜
kotomin_m
2
1.1k
伝えたい!オフラインのカンファレンスに参加するメリットと参加してから200%楽しむために実践してほしいこと
kotomin_m
2
6.9k
Other Decks in Programming
See All in Programming
データアナリストが行うDatabricksを活用したETLの自動化事例
shinoa
0
260
Ruby Function Composition
bkuhlmann
1
330
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
120
チーム力を高めるスクラム実践法:カンバン公開と課題攻略について - ニフティのスクラムトーク Vol. 2 - NIFTY Tech Talk #18
niftycorp
PRO
1
110
Netty Chicago Java User Group 2024-04-17
sullis
0
170
雑に思考を整理する技術と効能
konifar
58
28k
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
260
Blue/Greenデプロイの導入による 運用フローの改善
kudoas
1
360
Designing for tomorrow's programming workflows
honnibal
PRO
2
120
Elm 0.19.0 Changes
bkuhlmann
0
490
SwiftUI Performance 不要なViewの再描画と更新を抑える
bigamitiongit
1
160
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
Featured
See All Featured
The Language of Interfaces
destraynor
151
23k
Atom: Resistance is Futile
akmur
258
25k
Designing for humans not robots
tammielis
248
25k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
Rebuilding a faster, lazier Slack
samanthasiow
72
8.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
321
20k
We Have a Design System, Now What?
morganepeng
42
6.7k
Building a Scalable Design System with Sketch
lauravandoore
455
32k
Designing Experiences People Love
moore
136
23k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
356
22k
Raft: Consensus for Rubyists
vanstee
132
6.3k
From Idea to $5000 a Month in 5 Months
shpigford
377
45k
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