Eloquent withSum Extra Calculation: SubSelect with Raw Query

  Рет қаралды 11,668

Laravel Daily

Laravel Daily

Күн бұрын

In this video, I want to show a Laravel Eloquent trick on how to calculate the aggregated data before the aggregation.
Links I've mentioned:
- Spatie Ray: spatie.be/prod...
- Eloquent Subquery Enhancements in Laravel 6.0 laravel-news.c...
- Eloquent Performance Patterns: eloquent-cours...
- - - - -
Support the channel by checking out our products:
- Enroll in my Laravel courses: laraveldaily.t...
- Try our Laravel QuickAdminPanel: bit.ly/quickad...
- Buy my ready-made Laravel scripts: laraveldaily.g...
- Purchase my Livewire Kit: livewirekit.com
- Subscribe to my weekly newsletter: bit.ly/laravel-...

Пікірлер: 29
@intipontt7490
@intipontt7490 2 жыл бұрын
KZbin is marking my comments as spam it seems. The solution is to use the withAggregate function. Check the source code for the withSum method to see how it works. It' is the method called in the background.
@intipontt7490
@intipontt7490 2 жыл бұрын
withAggregate('items as total_price', 'sum(quantity * price)')
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Wow, didn't think about that one. Interesting how brain works: I really KNEW about this function, but when I get the chance to actually use it, my mind didn't "show" it to me :) Thanks!
@intipontt7490
@intipontt7490 2 жыл бұрын
@@LaravelDaily It's not commonly used. The same thing happens to me when I need to add a HAVING clause to my query. I always try to add it inside the WHERE clause, making it throw errors until someone on stackoverflow reminds me it exists for that specific reason.
@gianfrancobriones
@gianfrancobriones 2 жыл бұрын
Why did I just know this now 😭
@yashpatel7291
@yashpatel7291 2 жыл бұрын
I have 4 years of experience in laravel but I didn't use laravel sub query in my project. 🤭 I will implement that anyway this video impressive for me
@RedempleMarcelo
@RedempleMarcelo 2 жыл бұрын
GREATE IHAVE LARAVEL TIPS TOO
@AlbertCloete
@AlbertCloete 2 жыл бұрын
When those eloquent functions become too complicated I rather just write SQL. I feel it's easier to write and understand. Most people can read SQL easily.
@oxunjonrahmonov4728
@oxunjonrahmonov4728 Жыл бұрын
I searched for this thing for 1 hour, thank you very much
@AldhiAlbadri
@AldhiAlbadri 2 жыл бұрын
this channel is very complete discussing laravel..but i still stuck in english language..nice video nice course..
@spartacusyusuf9989
@spartacusyusuf9989 2 жыл бұрын
I used subSelect but my problem is ordering the result or searching the column when it has an alias
@renwar
@renwar 2 жыл бұрын
Not the elegant way, but I am using this: use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Query\Expression; Order::withSum(['items' => function (Builder $query) { $query->select(new Expression('sum((`items`.`quantity`) * (`items`.`price`)) AS totalPrice')); }], 'totalPrice');
@intipontt7490
@intipontt7490 2 жыл бұрын
Isn't new Expression(...) the same as DB::raw(...) ?
@renwar
@renwar 2 жыл бұрын
@@intipontt7490 you can't use DB::raw inside "withSum" function.
@firdavs.ibodullayev
@firdavs.ibodullayev 2 жыл бұрын
Thanx for video
@fajarsetiawansiagian
@fajarsetiawansiagian 2 жыл бұрын
thanks for this informatin. i dont think if we can use subquery for this
@aneeshn933
@aneeshn933 2 жыл бұрын
machan onnum manasilayilla
@Xadios7
@Xadios7 2 жыл бұрын
Thank youuu
@NotBeHaris
@NotBeHaris 2 жыл бұрын
Can anyone help me in this query. That i have posts and views so now i want to show popular day and week when most views comes.
@free2idol1
@free2idol1 2 жыл бұрын
can you show your migration and eloquent relation?
@RedempleMarcelo
@RedempleMarcelo 2 жыл бұрын
tHANK YOU
2 жыл бұрын
Is this method will cause n+1 issue?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
It shouldn't. But, to be honest, I didn't check.
@dearlorddd
@dearlorddd 2 жыл бұрын
@@LaravelDaily I tested it, it is doing a subselect in the query.
@RedempleMarcelo
@RedempleMarcelo 2 жыл бұрын
THANK YOU
@fendysusanto4873
@fendysusanto4873 2 жыл бұрын
@@LaravelDaily btw, how to group by(some key) the result? thanks
@azzamydev
@azzamydev Жыл бұрын
@@LaravelDaily In my project, n+1 problems occurred
@LostInAutism
@LostInAutism 2 жыл бұрын
With my limited knowledge of Laravel, wouldn't it work if you included the table prefixed to the columns? Ex: items.quantity * items.price Appreciate the videos! You always post videos that relate to my current projects and I appreciate that 😊
@RedempleMarcelo
@RedempleMarcelo 2 жыл бұрын
THANK YOU
Laravel Eloquent: Deeper Relationships with One Query
10:37
Laravel Daily
Рет қаралды 141 М.
Faster Eloquent: Avoid Accessors with Foreach
9:35
Laravel Daily
Рет қаралды 53 М.
LIFEHACK😳 Rate our backpacks 1-10 😜🔥🎒
00:13
Diana Belitskay
Рет қаралды 3,9 МЛН
Spongebob ate Patrick 😱 #meme #spongebob #gmod
00:15
Mr. LoLo
Рет қаралды 18 МЛН
The day of the sea 😂 #shorts by Leisi Crazy
00:22
Leisi Crazy
Рет қаралды 2,2 МЛН
High performance code with laravel eloquent subquery
21:41
WebDevMatics
Рет қаралды 12 М.
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 84 М.
Can You Make YouTube From Scratch Using Only Bash?
17:51
7 Powerful Databases Python Developers Should Know
23:55
ArjanCodes
Рет қаралды 27 М.
RTK Query in React (Complete Tutorial)
29:23
Cosden Solutions
Рет қаралды 8 М.
Laravel Advanced Subqueries | Subquery Ordering
8:59
Tony Xhepa
Рет қаралды 1,2 М.
I built 10 web apps... with 10 different languages
14:23
Fireship
Рет қаралды 1,6 МЛН
Optimizing Eloquent: Running Out Of Memory?
6:11
Laravel Daily
Рет қаралды 43 М.
The Tools I Use to Build Products in Laravel
19:00
Josh Cirre
Рет қаралды 31 М.