Laravel Polymorphic Many-to-Many Performance: Query Pivot Table

  Рет қаралды 12,020

Laravel Daily

Laravel Daily

11 ай бұрын

One "trick" that you can use when querying data and filtering by polymorphic relations.
[Full tutorial] Laravel: BelongsToMany or Polymorphic Relations? Practical example laraveldaily.com/post/laravel...
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.com/courses
- Laravel QuickAdminPanel: quickadminpanel.com
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-archive.com/hom...
- My personal Twitter: / povilaskorop

Пікірлер: 32
@juanpineda-montoya
@juanpineda-montoya 11 ай бұрын
Great Stuff, and performance gains are huge!
@MickDavies
@MickDavies 11 ай бұрын
Great tip! I’ve learned something new 🎉
@adilizm704
@adilizm704 11 ай бұрын
wonderful and easy to understand thanks a lot sir
@francescoleto2823
@francescoleto2823 11 ай бұрын
Good advice 😊
@EliasGikonyoMogul
@EliasGikonyoMogul 11 ай бұрын
Awesome!
@killionaire175
@killionaire175 11 ай бұрын
There is no such thing as a many-to-many relationship from the eyes of a true software architect. There is only one-to-many relationships - even if that means creating pivot models.
@UwU-uq9pq
@UwU-uq9pq 11 ай бұрын
dammm I dont understand.... why it's that?
@killionaire175
@killionaire175 11 ай бұрын
@@UwU-uq9pq A many-to-many relationship has an intermediary (pivot) table that holds the foreign IDs. Rather than ignore the pivot that sits between the relationship, you should see the database blueprint from the perspective if including the pivot model. At that level, you are always dealing with one-to-many (or has-many) relationships.
@UwU-uq9pq
@UwU-uq9pq 11 ай бұрын
@@killionaire175 by saying that the laravel model many to many relationship actually has a pivot table to do that? even if we didnt create a pivot table? or do handle many - many relationship we better to use a pivot table?
@KG-id3hk
@KG-id3hk 11 ай бұрын
​@killionaire175 In an ideal world, you are right, but, when working with sensitive data, its inevitable and you cant afford to lose links between data because of a silly detachable mistake.
@InMemoryOfNeo
@InMemoryOfNeo 10 ай бұрын
nice tip, thanks.
@cellocarlos
@cellocarlos 7 ай бұрын
Awesome
@user-fy2hq3xb2z
@user-fy2hq3xb2z 11 ай бұрын
Nice ❤
@GringoDotDev
@GringoDotDev 11 ай бұрын
Truthfully this is why I generally prefer to use Eloquent more as a query builder than an ORM.
@bulent2435
@bulent2435 11 ай бұрын
Nice.
@GergelyCsermely
@GergelyCsermely 11 ай бұрын
Thanks
@zikriendisyahmunandar4037
@zikriendisyahmunandar4037 11 ай бұрын
cool vids
@wadecodez
@wadecodez 11 ай бұрын
Wherehas is usually an indicator to me that a query needs to be optimized.
@mahmoudadel8313
@mahmoudadel8313 11 ай бұрын
Great, but for an better performance don't use assignable_type as a string make it integer and with adding an combined index with task_id and assignable_id and assignable_type u will get a more better performance query
@insaneskullgaming
@insaneskullgaming 11 ай бұрын
I prefer this, no need to store model with namespace, just make it types and map them when you need to query further.
@mahmoudadel8313
@mahmoudadel8313 11 ай бұрын
@@insaneskullgaming it's all about database not what we prefer when using polymorphic relationship model type when be string and index it's not what working fine in another hand integer working perfectly with index
@free2idol1
@free2idol1 11 ай бұрын
@@mahmoudadel8313 Hi, if making the assignable_type as integer, then how can we map them? by creating constant or Enum? Can you please elaborate? Thanks.
@ricohertha3537
@ricohertha3537 11 ай бұрын
I guess, in your solution there are to much repeatiations in the column "assignable_type". It's a more cleaner design If there is a separate assignment-table for each type. to merge these different assignment-tables back into one normalized presentation (if needed) you can simply create a db-view "assignables". In this view can declared how the different assignment-tables (virtual) merged back into one normalized presentation (by union). Also it's a good idea to create multiple views for multiple use-cases - prevent godviews. like in oop you can combine "abstract" views to more specialized ones. benefits are: use much less storagespace, much better index usage and maintaining (remember: "or" is an index-killer), less table-locks at read/write/delete operations, allows using of foreign-keys to protect data-consitence and use traversal update/delete ... views hides complexity all together results in: much better read/write performance especialy if count of assignments grows up over the time (over many years), query-optimizer can cache queryplans, separation of concerns and decoupling (no tech debt -> all types must not share the same assignment-structure, but can normalized to a common structure by view). better overview and documentation for old and new team members I know, maybe its sounds greatly exaggerated for this simple example. But it's so important, to invest in a good architectur at the beginning of a softwareproject. Try to avoid "i refactor this part later" because often "later equals never" "quick and dirty" must have a very good reason - it can be the beginning of software-degeneration and can be so expensive in the future (tech dept). "there was no time" is a lame excuse!!! building good architectures needs time / same like write good books needs time. at the end you have the responsibility for your work - and maybe you must maintain it for a long time. You should be proud about what your have created - you shouldn't have to be ashamed of it And please use a uid for every datarow (auto-id, uuid ...)! Also every book has page-numbers ;)
@cellocarlos
@cellocarlos 7 ай бұрын
The created db-view "assignables" would be a union of the other tables? In terms of performance, querying this view is better than querying the polymorphic table?
@resort16
@resort16 11 ай бұрын
Just use joins, not whereHas. And performance will be even better. Try it.
@mohamadcheaib
@mohamadcheaib 11 ай бұрын
Great 👍 , Isn't even faster if we move the repetition of the condition where assignable_id =... outside to be and whereIn('assignable_id',[auth()->id, auth()->user()->group_id, auth->user()->position_id])?
@andreivirgillungeanu5542
@andreivirgillungeanu5542 11 ай бұрын
I had the same idea, probably is.
@me1ales
@me1ales 11 ай бұрын
but you will have wrong records same id like group_id in positions or users table, same id like user_id in groups or positions etc
@AndersFloor
@AndersFloor 11 ай бұрын
If auth()->id() = 1 and auth-()>user()->position_id = 2, your solution would also return the tasks of the user with auth()->id() of 2.
@mohamadcheaib
@mohamadcheaib 11 ай бұрын
I am not a fan of polymorphic, because it forces the database structure to follow Laravel models, i prefer to make the db structure independent of the framework that i am working on.
@user-tb4ig7qh9b
@user-tb4ig7qh9b 11 ай бұрын
I think have solution will be better and the solution just make index on the feild you want search by
Laravel Pivot Tables: Simple to Advanced Many-to-Many
12:24
Laravel Daily
Рет қаралды 119 М.
Laravel Eloquent: Deeper Relationships with One Query
10:37
Laravel Daily
Рет қаралды 137 М.
ТАМАЕВ УНИЧТОЖИЛ CLS ВЕНГАЛБИ! Конфликт с Ахмедом?!
25:37
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 106 МЛН
Каха и суп
00:39
К-Media
Рет қаралды 5 МЛН
Laravel: BelongsTo vs Polymorphic? Let's Test!
14:09
Laravel Daily
Рет қаралды 26 М.
Do This Instead Of Representing State With Booleans
12:23
Joy of Code
Рет қаралды 114 М.
Static Methods in Laravel/PHP: When and How?
10:39
Laravel Daily
Рет қаралды 16 М.
Creating Many-To-Many Relationships in Flask-SQLAlchemy
7:28
Pretty Printed
Рет қаралды 42 М.
Eloquent: Filter Pivot by Array of Records
16:14
Laravel Daily
Рет қаралды 7 М.
Eloquent Polymorphic Relations: Properly Explained
9:56
Laravel Daily
Рет қаралды 40 М.
I Never Want to Create React Tables Any Other Way
5:40
Josh tried coding
Рет қаралды 243 М.
Best father #shorts by Secret Vlog
0:18
Secret Vlog
Рет қаралды 21 МЛН
What did I eat? 🤪😂 LeoNata Best #shorts
0:19
LeoNata Best
Рет қаралды 1,1 МЛН
Who has won ?? 😀 #shortvideo #lizzyisaeva
0:24
Lizzy Isaeva
Рет қаралды 62 МЛН
ToRung short film: 🙏get a free meal🤤
0:41
ToRung
Рет қаралды 27 МЛН
УТОПИЛА ДОРОГУЩИЙ ТЕЛЕФОН 😭
1:01
Аня Панкова
Рет қаралды 9 МЛН