Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Laravelでタグ機能を作ってちょっとハマった話
Search
fortkle
July 18, 2015
Programming
3.9k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Laravelでタグ機能を作ってちょっとハマった話
Laravel Meetup Tokyo Vol.7
fortkle
July 18, 2015
More Decks by fortkle
See All by fortkle
無駄な物をなるべく作らないリプレイス戦略 / replace-strategy-phperkaigi2021
fortkle
1
2.7k
フルリモート時代のカンバン運用 / kanban-operation-in-remote
fortkle
0
800
GitHub Actionsで始めるPHPアプリケーションのCI実践入門 / ga-phperkaigi2020
fortkle
3
7.2k
余裕を生み出すコードレビュー 〜レビュイー編〜 / code-review-phpcon-2019
fortkle
8
7.6k
「設計振り返り」を始めてみようと思っている話 / architecture reflection
fortkle
3
640
「ママ向けNo.1アプリ」の 更なる成長を支える仕組み / startup-engineer-night-connehito
fortkle
2
370
良いテストデータ、悪いテストデータ / testdata-antipattern
fortkle
4
7k
BackstopJSで始める CSSリグレッションテスト / backstopjs-css-test
fortkle
0
1.6k
PhpStorm導入アンチパターン / phpstorm-anti-pattern
fortkle
0
2.2k
Other Decks in Programming
See All in Programming
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
370
すこし踏み込む CancellationToken
htkym
2
1.7k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.1k
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
360
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
340
Java 27新機能 / Java 27 new features
kishida
2
210
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
150
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
450
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
160
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
950
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
130
JRuby: Past, Present, and Future
headius
0
230
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1369
210k
Designing for Timeless Needs
cassininazir
1
520
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
280
Color Theory Basics | Prateek | Gurzu
gurzu
1
480
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
210
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
The SEO Collaboration Effect
kristinabergwall1
1
590
Art, The Web, and Tiny UX
lynnandtonic
304
22k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
67k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
840
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
400
Transcript
Laravelでタグ機能を作って ちょっとハマった話 Laravel Meetup Tokyo Vol.7 @fortkle
自己紹介 • 高野福晃 – Istyle, inc. 開発基盤改善チーム – Twitter: @fortkle – Github:
fortkle
アジェンダ 1. タグ機能の仕様 2. attach()の罠 3. sync()がグー便利
1.タグ機能の仕様
タグ機能を作ってみる • 趣味で作っている情報共有ツール – https://github.com/fortkle/owl • 利用者は記事を投稿することができる • 記事に対してタグを付けたい!
タグ機能のテーブル設計 • 調べてみると幾つかのやり方が見つかった • 下記の記事に詳しく載っているので参考にした
タグ機能のテーブル設計 • 今回は「Toxi法」という設計を採用 • Toxi法は3つのテーブルから成る • 中間の「tagmap」テーブルでbookmarkテーブルとtag テーブルが関連付けられ、bookmarkデータとtagデー タは多対多の関係になる。
Laravelでのリレーション • 今回はEloquentを使う • 記事をitemsテーブル、タグをtagsテーブルとする • 中間テーブルとしてitem_tagテーブルを作る • 記事とタグは多対多の関係なので下記のように定義
2.attach()の罠
多対多の関連の操作 • LaravelでEloquentを使っている場合、簡単に多対多 の関連を操作することができる • 例えば、ID=1の記事に、ID=1と2のタグを単純に追加 する場合、attachメソッドを使う • attach()にはID(配列でもOK)を渡してあげる $tag_ids
= [1,2]だとする。
多対多の関連の操作 • 期待通りになっている!
多対多の関連の操作 • …と思ってタイトルを少し変えて更新してみたら • 同じデータがitem_tagテーブルにinsertされてるー! • attachメソッドは存在チェックをせずに問答無用で insertしてくる!
3.sync()がグー便利
多対多の関連の操作 • attach()の他に、多対多のモデルを削除するdetach() がある • さらに、attach()とdetach()の機能を併せ持ち、存在 チェックまで良しなにしてくれるsync()がある! • sync()を実行すると、そのモデルに対する中間テーブ ルは配列で指定されたIDだけになる!
$tag_ids = [1,2]だとする。
多対多の関連の操作 • 一度item_tagテーブルのレコードを削除して、記事の 登録・更新をやってみると • 今度は正しく更新されました!
まとめ • Eloquentだとさくっとモデルのリレーションが定義で きて楽ちん。 • sync()がグー便利。
ご静聴ありがとうございました