Laravel: BelongsTo vs Polymorphic? Let's Test!

  Рет қаралды 26,465

Laravel Daily

Laravel Daily

4 жыл бұрын

I get a lot of questions about database structure, and in this video I will practically test two different DB structures and we will try to see which is better: belongsTo with three columns, or polymorphic relations?
[00:50] Which DB structure to choose?
[02:51] Which DB table is bigger in size?
[03:44] List of entries: which is faster?
[05:45] Querying by ID: which is faster or more convenient?
[08:00] Querying by name of the relationship
[10:24] Adding another field: where Polymorphic "shines"
[12:06] Polymorphic is not actually a relationship
- - - - -
Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
Enroll in my Laravel courses: laraveldaily.teachable.com

Пікірлер: 33
@Atiradeonvideo
@Atiradeonvideo 2 жыл бұрын
Always really good when a video is reflecting a day-to-day kind of situation at work. These decision (database structure, coding style) are happening a lot under the hood and a good developer should always address all these long-term considerations as you do here. Nice job!
@ashrafuzzamashraf8854
@ashrafuzzamashraf8854 11 ай бұрын
Good benchmark video. Thanks
@ejaadhulab8924
@ejaadhulab8924 2 жыл бұрын
this is exactly what I am looking for. Thank you very much for sharing daily tips. Although the tutorial is old the content is not outdated.
@3mro_coding
@3mro_coding 2 жыл бұрын
This review video didn't get what it deserves. Am sharing this to all social networks this really quality content. Thanks for sharing.
@maiconmva
@maiconmva 4 жыл бұрын
but if you add a index key in class name column, will be much faster
@Construction-agencyCoUk
@Construction-agencyCoUk 4 жыл бұрын
fantastic video - thanks 🙏
@leonardoldr
@leonardoldr 3 жыл бұрын
I know it's an old video, but in fact for an application that have to be maintained for a long time polymorphic seems to add the inconvenience of "guessing". Pretty much any developer knows what a foreign key is, but maybe just the advanced will realize the polymorphic. Also, would be fine to have a real-world test with the "real" database modelling with 3 tables vs 1 poly and each of them with millions of rows. But, as always very helpful, your videos are helping me a lot! :)
@shire7949
@shire7949 3 жыл бұрын
Very valuable information
@maxralph01
@maxralph01 Жыл бұрын
Very informative!
@ahmedabdnaser1582
@ahmedabdnaser1582 2 жыл бұрын
thank you, this video is very informative. keep going
@thatsenam9183
@thatsenam9183 3 жыл бұрын
Really Helpful
@sonant_bwolfe
@sonant_bwolfe 3 жыл бұрын
"This table full of strings is bigger than this table full of integers, I'm not sure why when I export both as utf8 text they're the same size" :D
@nabeelyousafpasha
@nabeelyousafpasha 4 жыл бұрын
Do you feel it unnecessary to query multiple times via WITH method rather than JOIN? However some may find join slower but we can manipulate data at db level in a single query using multiple tables with JOIN. DB transactions cost lesser (if i am right). What are your suggestions regarding this
@PovilasKorop
@PovilasKorop 4 жыл бұрын
It's a personal preference, I tend to use all Eloquent possibilities and not Query Builder with join(), whenever possible. Doing it directly with join() sometimes may be quicker, sometimes not. It depends on the situation.
@Jossnaz
@Jossnaz 3 жыл бұрын
I appreciate your videos. I keep thinking as well that you focus a bit too much about "shorter syntax". Just because you can mke it shorter, doesnt mean you should. Readability is king, reading the couple ifs vs that string replace? For me ifs way easier to read. As well imagine you need translations. Your ide will help you find the references. And its easy to add another if. For example i dont like laravel resources a lot either because i like to be able to search for a problematic http call in my code. If you use resource you wont find it any more. Its shorter, but at what cost?
@devmaster8923
@devmaster8923 4 жыл бұрын
how to install the bar like yours? where you can see the result?
@PovilasKorop
@PovilasKorop 4 жыл бұрын
It's called Laravel Debugbar, Google how to install it
@helluci6449
@helluci6449 Жыл бұрын
6:30 also breaks the SOLID principles, right? Is there a way to prevent that?
@cxian
@cxian Жыл бұрын
What do you call on that Laravel developer console on the bottom of the page? Is that part of the Quick Panel?
@LaravelDaily
@LaravelDaily Жыл бұрын
composer require barryvdh/laravel-debugbar
@cxian
@cxian Жыл бұрын
@@LaravelDaily Thank you sir!
@bunwong9718
@bunwong9718 4 жыл бұрын
What is the name of the DEVTOOL bar under the browser?
@PovilasKorop
@PovilasKorop 4 жыл бұрын
Laravel Debugbar github.com/barryvdh/laravel-debugbar
@dusanjovanovski8752
@dusanjovanovski8752 Жыл бұрын
Its a package for debug
@evilwizard7931
@evilwizard7931 Жыл бұрын
Does having multiple foreign key fields in this case break normalisation? I mean how can the database enforce that only 1 of the fields is populated & the other 2 are null? Also, A big thanks you for your tips & knowledge sharing
@LaravelDaily
@LaravelDaily Жыл бұрын
From what I understand, yes, polymorphic relationship breaks the normalization. But normalization isn't a sacred rule, in my opinion.
@evilwizard7931
@evilwizard7931 Жыл бұрын
@@LaravelDaily I meant the other way, the database can enforce the relationship with the other tables, but how can the table containing multiple foreign key fields as primary relation. What is stopping a record being linked to all 3 other tables is what I'm asking I guess & has no bearing on Laravel/Eloquent. It's as sacred as SOLID, something to strive for, but don't break your back doing it, even the fundamental laws of the universe do it the easy/lazy way :)
@LaravelDaily
@LaravelDaily Жыл бұрын
Well, polymorphic relationship is NOT the database relationship, it doesn't have foreign keys or other DB level constraints. If you go for polymorphic, you kinda forget DB constraints and rely on Laravel functionality.
@iordanistsapanidis4021
@iordanistsapanidis4021 4 жыл бұрын
Why not use db queries?? easier with() than join() but faster
@pola587
@pola587 4 жыл бұрын
Not recommended for readability
@iordanistsapanidis4021
@iordanistsapanidis4021 4 жыл бұрын
@@pola587 what can't you understand exactly??
@TheBaltLT
@TheBaltLT 6 ай бұрын
what value your talking head adds to this tutorial apart distraction?
@wayne_xyz
@wayne_xyz 6 ай бұрын
So that you can see and remember the actual human being who is making efforts to teach everyone who needs teaching. You ungrateful asshole
Eloquent Polymorphic Relations: Properly Explained
9:56
Laravel Daily
Рет қаралды 40 М.
Laravel Polymorphic Many-to-Many Performance: Query Pivot Table
5:54
Children deceived dad #comedy
00:19
yuzvikii_family
Рет қаралды 8 МЛН
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 32 МЛН
Laravel 6 Advanced - e3 - Polymorphic Relationships
21:33
Coder's Tape
Рет қаралды 75 М.
Eloquent or Query Builder: When to Use Which?
5:48
Laravel Daily
Рет қаралды 87 М.
Eloquent Where ENUM vs Int Foreign Key: Speed Benchmark
8:53
Laravel Daily
Рет қаралды 3,9 М.
Exceptions in Laravel: Why/How to Use and Create Your Own
12:18
Laravel Daily
Рет қаралды 86 М.
P7 Amazing Gadgets, Kitchen Utensils, Home cleaning, Inventions, Ideas part 4
0:10
Popping green grap jelly!- got these in Japan
0:16
Priya kumari
Рет қаралды 23 МЛН
Choose a car for Mom ♥️ #shorts #trending #viral #cars
0:17