No video

Laravel Checkout: Database Transactions and Validation

  Рет қаралды 28,888

Laravel Daily

Laravel Daily

Күн бұрын

This video is a small live-coding demo project of the case where one controller method is responsible for many DB actions and should be wrapped in a transaction.
Official docs: laravel.com/do...
- - - - -
Support the channel by checking out our products:
- Try our Laravel QuickAdminPanel: bit.ly/quickad...
- Enroll in my Laravel courses: laraveldaily.t...
- Purchase my Livewire Kit: livewirekit.com
- Subscribe to my weekly newsletter: bit.ly/laravel-...

Пікірлер: 71
@JohnnyBigodes
@JohnnyBigodes 3 жыл бұрын
Povilas you are not making silly mistakes. It is much better when you also see other struggling... Nobody know everything and from mistakes we all learn it better.
@2509killer
@2509killer 3 жыл бұрын
great video! The „mistakes“ make it all the more exciting and instructive. I would like to have your skills
3 жыл бұрын
Thank you for the good information. It's very explanatory. It's much better to make mistakes because you're also teaching how to find fault.
@admirhrustic6149
@admirhrustic6149 3 жыл бұрын
This channel is just great! I really appreciate your tutorials, sir!
@poplach
@poplach 3 жыл бұрын
on 2:18 I was 'oh my god, n+1 detected'! 5:30 when querying by ids, you can use find or findMany (find works the same way as findMany if you pass an array). The second argument is the required columns. So $products = Product::find($card->pluck('product_id'), ['id', 'quantity_left'])->pluck('quantity_left','id') will be same Just in case. P.S. $order->increment() also fires multiple times, could be done outside of foreach() loop.
@J87NL
@J87NL 3 жыл бұрын
5:30 Maybe I’m wrong but since there is an Cart::with(‘product’), don’t you have the quantity_left in the result of $cart? Maybe with a different association since you’re working with a jointable. I might overlook something but it feels like you can get away with another query less.
@wildangunawan7657
@wildangunawan7657 3 жыл бұрын
Correct. He doesn't need to query for product again since he already got it. That's why, as he said in the video, refactoring is important. At least got things right first then we can improve another time ;)
@LaravelJutsu
@LaravelJutsu 3 жыл бұрын
Maybe my question is stupid, but wouldn't it be better to create a many-to-many relationship between carts and products ? Thank you very much for these daily videos, you are a great inspiration for me ! Best wishes from France 🇨🇵
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Maybe it is suitable too, you can try it. The relationships weren't the point of this video, so I didn't emphasize it.
@GergelyCsermely
@GergelyCsermely 3 жыл бұрын
Thanks. Very interesting. Theoretical question: In a real project I guess You would check stocks before goods are registered in the cart. In this example if we have a busy shop and a lot of items in the cart it could be a problem to check the stocks before the loop (?) maybe a concurent order could get the stock during we process the order (?)
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes you're probably right, Sorrawut pointed out the same thing in another comment.
@lucasj.pereira4912
@lucasj.pereira4912 3 жыл бұрын
Hi Povilas, not about the video, but Laravel Examples. Suggestion: A page where we can see when you add new packages to the site or a notification. It really helped me already, but it is hard to keep track of new things in the website. Something like a Log of new packages added, or even a email notification and maybe, a video about this subject (Audit log). Thank you! Great work as always. Cheers from Brazil.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Thanks Lucas. I thought about it, but I'm not building it as a "news feed" where I would constantly add new examples, I guess I will add some 1000 examples or so and will more-or-less stop adding new things, or add small amount. So not planning to add news notifications at the moment, I'm planning it to be used as "wikipedia" or "google", like on-demand search when you need something.
@msdeav
@msdeav 2 жыл бұрын
So we basically need to use try-catch every time?
@thelostrider1
@thelostrider1 3 жыл бұрын
Awesome video, always nice to learn some new eloquent/database methods!! The video left me with a question. The checkout just let's the user choose the payment method. So my doubt is: 1) should we remove the itens (quantity) from the database before he actually pays, since there are payments methods that allow you to pay until 3 days for example? 2) Or could we remove the quantity but in some sort of standby? Like adding in the order table some "did he pay" (boolean) collumn and "expiration date" column?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
There's no one easy way to answer it, I guess I need to shoot a follow-up video with real payment scenario and possible cases like yours.
@thelostrider1
@thelostrider1 3 жыл бұрын
@@LaravelDaily That would be really nice, some information about how to handle a real payment scenario. Thanks!!
@snipiba
@snipiba Жыл бұрын
for bigger solutions with storage etc, there are probably something another as solution. for example, as you wrote, stock syncs where putting to cart creates a temporary "on hold" decrementation to avoid ordering and allows users to create a preorders, etc etc. stock counts may be decreased only when products are shipped out. but, as i mentoined, it was a complex stock solution.
@snipiba
@snipiba Жыл бұрын
question here... on line 70 fetches with eager product, right? then on condition for checking quantity u can skip whole fetching products by ids and instead of use a check for if($cartProduct->product->quantity_left < $cartProduct->qty) ... or im wrong?
@ahmerayaz8825
@ahmerayaz8825 9 ай бұрын
Why are we querying the products separately in the first step? Can't we use $cart to get the products as we already have used eager loading to get the related products? $products = $cart->pluck('product.quantity_left', 'product.id')->all();
@abdul-rehman
@abdul-rehman 3 жыл бұрын
Thank You, sir you are doing a great job. But I have a silly question (maybe). Assume that we have a busy shop, and multiple users want to buy the same product at the same time. Then what if we have less quantity of that product than the total product orders. And how we can manage the quantity_left and error messages? And in case if a user left with the cart filled and didn't proceed to the payment process.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Well, in a busy shop, the scenario would be even different and more complex, like in ticketing systems people "reserve" the tickets and then they are released to public if not finished purchasing, with some script every 15 minutes, for example.
@abdul-rehman
@abdul-rehman 3 жыл бұрын
​@@LaravelDaily you are right, I think would have an actual_quantity_left column for final purchase orders and a quantity_left column for temporary up/down. We also can use Jobs and a queue worker for order-jobs.
@abdul-rehman
@abdul-rehman 3 жыл бұрын
@@LaravelDaily did you uploaded any video or course about Laravel Jobs and Queues on the production server?
@bayuadi1766
@bayuadi1766 2 жыл бұрын
sorry, if u can show proccess in blade checkout, i think this so insightfull video. Please give detail for blade with jquery sir thanks
@abdulwadoodkhan826
@abdulwadoodkhan826 2 жыл бұрын
How to display cart items in table using livewire??
@zkabadu
@zkabadu 2 жыл бұрын
Also: you need to wrap the validation in the transaction. What if some other guy orders the items between the validation and your own order? Than the validation was useless and wrong.
@MJacksi
@MJacksi 2 жыл бұрын
Thank you!
@hassanfazeel2354
@hassanfazeel2354 3 жыл бұрын
Awesome tips!
@aomo5293
@aomo5293 3 жыл бұрын
Can y show us an example of installment payment.
@yaw8821
@yaw8821 3 жыл бұрын
hi there, can you please explain solid principle or model functions usage in laravel? please also provide some examples.
@user-qp6pu9ge6g
@user-qp6pu9ge6g 3 жыл бұрын
Thanks for your video. But in the video it seems that you can create an empty order without any exception?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes, good catch. I didn't work on all possible exceptions and cases, wanted to just show the main point. If I worked on all scenarios in all my demos, my videos would be 1-hour long and not 5-10 minutes.
@hariomnagar5606
@hariomnagar5606 3 жыл бұрын
Thank you so much 💐💐
@jagrutivaru5827
@jagrutivaru5827 3 жыл бұрын
Hi Sir can you please guide me for, how can I update the cart items for example you have make like everytime you make a cart update items. Some items will be added and some should delete from cart for some increase or decrease the quantity. Will you please guide me how to do so. I can share example for my question also let me know if it's required.
@renwar
@renwar 3 жыл бұрын
Awesome! Thanks
@rahman_abdu
@rahman_abdu 3 жыл бұрын
how to add progress bar while importing excel
@bboydarknesz
@bboydarknesz 3 жыл бұрын
We love how every developer could make errors, makes more realistic haha.. Anyway is live wire rendering take resources in server? How about lavarel components? Is it live like live wire? Sorry for my many questions
@maximgasai1349
@maximgasai1349 3 жыл бұрын
Livewire renders content on a server-side, then just replaces HTML with JavaScript
@giacomogaravaglia6742
@giacomogaravaglia6742 3 жыл бұрын
It could be nice if you show us a way to deploy a laravel app in production. Some times ago you asked us how we do it, now it's your turn?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
I've written about it, in a very detailed way, long time ago: laraveldaily.com/how-to-deploy-laravel-projects-to-live-server-the-ultimate-guide/ Also, search for "deploy" on this channel. Realistically, deployment depends on so so so many individual factors, that my way of deployment will not be suitable for 90% of the situations for others.
@sowmik.reborn
@sowmik.reborn 2 жыл бұрын
I have a question. Why most of the professionals and even the developers of Laravel prefers TailwindCSS and VueJS than the others?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
It's a personal preference, they just like it more. Also, both Tailwind and Vue creators are active in Laravel community, with joint collaborations and specific content for how to use it in Laravel. For example, Bootstrap or React creators don't participate in Laravel community.
@ChangeYourLifeForever
@ChangeYourLifeForever Жыл бұрын
awsm
@ibrahimkhalaf4656
@ibrahimkhalaf4656 3 жыл бұрын
Please I need your help in a task to get a job the task is about appointments
@shareyarzaheer2073
@shareyarzaheer2073 3 жыл бұрын
Can we use DB::beginTransaction(); and commit and rollback control in our hand with try catch finally ?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes.
@shareyarzaheer2073
@shareyarzaheer2073 3 жыл бұрын
I tried that but it's not working and create record whereas i am rollbacking on any sort of exception happen . Can you give little guide on that in your next video would be great 🙂
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Not sure if I would add anything mode useful than default Laravel docs in this case. It's also about debugging your own personal code, I can't do the debugging for you, sorry.
@nekozu_
@nekozu_ 3 жыл бұрын
Create a video about design pattern in Laravel framework please
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Which design pattern? There are dozens of them. Also, look at the examples in my laravelexamples.com - section "Patterns"
@nekozu_
@nekozu_ 3 жыл бұрын
@@LaravelDaily repository and service
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Search the channel: kzbin.info/door/TuplgOBi6tJIlesIboymGAsearch?query=service
@carlosaires2688
@carlosaires2688 3 жыл бұрын
This IDE is PHPStorm? If are, whats the theme it is. Thank you.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes, Material Darker
@alisaprima2142
@alisaprima2142 2 жыл бұрын
Hello Sir. I have a question. Is storing cart data in database better than storing data in session?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Personal preference.
@bidhanbaniya7605
@bidhanbaniya7605 3 жыл бұрын
How to dynamically switch the database in laravel hoping for your reply? 🥺
@ward7576
@ward7576 3 жыл бұрын
I tell that anyone of age of comprehension CAN become a programmer, but if you cannot grasp the concept of using Google when it is free and available for most (even alt search engines will help you greatly), then you will struggle too much for your own good (but you'll help better devs with getting a job fixing your errors, sort of a 'win - win' situation; call me names, I don't care - that's the truth). I advise you to take your comment, cut off the part of "hoping for your reply" and put it in the search input on Google. First result, done. Do your own f.. research & RTFM!
@mayyadanartun168
@mayyadanartun168 2 жыл бұрын
can you share the code for this video?
@soniablanche5672
@soniablanche5672 3 жыл бұрын
How would you handle race condition in this example? If 2 people are buying the same product at same exact time wouldn't this cause problem?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
For race condition, I would introduce the "reservation" of the products and then release the reservations after they finalize the purchase or become available if the transaction isn't complete in, like, 15 minutes.
@zkabadu
@zkabadu 2 жыл бұрын
Uses with('product'), doesn't use it, loads it in the line below again :'D
@bimanugraha5526
@bimanugraha5526 2 жыл бұрын
Can you share the code, please?
@andreich1980
@andreich1980 2 жыл бұрын
You can skip querying products, you can use Cart products relationship for everything, can't you? One query to find product, then decrement quantity, all in a loop. Incrementing order total quantity is also in loop? Not good. And you can make $cart->delete(), since you already have it.
@DynamaticGamee0000
@DynamaticGamee0000 3 жыл бұрын
Hey I like your video can u make a tutorial for running your project on .test
@LaravelDaily
@LaravelDaily 3 жыл бұрын
For that, I just use Laravel Valet locally, and it does it automatically for me
@DynamaticGamee0000
@DynamaticGamee0000 3 жыл бұрын
@@LaravelDaily thanks
@nftsparatodos1274
@nftsparatodos1274 3 жыл бұрын
Nice videos every day, are you thinking in create new courses in your teachable page ?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes, I'm just taking a small summer break, but the next plan is the course about flutter mobile apps with Laravel API, coming in September.
@elyambay
@elyambay 3 жыл бұрын
That would be awesome
Junior Code Review: Laravel Routes, Middleware, Validation and more
19:57
КАКУЮ ДВЕРЬ ВЫБРАТЬ? 😂 #Shorts
00:45
НУБАСТЕР
Рет қаралды 3,3 МЛН
а ты любишь париться?
00:41
KATYA KLON LIFE
Рет қаралды 3,3 МЛН
Schoolboy Runaway в реальной жизни🤣@onLI_gAmeS
00:31
МишАня
Рет қаралды 3,4 МЛН
DaVinci Resolve 19 Speed Editor Firmware Update
7:29
Laravel: Create Public API with Cache and Rate Limits
12:18
Laravel Daily
Рет қаралды 45 М.
Laravel. Сервисы, контракты и внедрение зависимостей
29:18
Lectoria. Обучение веб-разработке.
Рет қаралды 22 М.
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 83 М.
DHH discusses SQLite (and Stoicism)
54:00
Aaron Francis
Рет қаралды 59 М.
Build a shopping cart with Laravel Cashier and Vue
55:07
Andrew Schmelyun
Рет қаралды 47 М.
Laravel solved race conditions
14:13
Aaron Francis
Рет қаралды 15 М.
Eloquent Performance: TOP 3 Mistakes Developers Make
7:59
Laravel Daily
Рет қаралды 48 М.
.NET Developer miserably fails at Laravel PHP
52:23
Raw Coding
Рет қаралды 13 М.
Открытое собеседование на PHP Мидл разработчика
36:30