Laravel Checkout: Database Transactions and Validation

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 71

  • @JohnnyBigodes
    @JohnnyBigodes 3 роки тому +22

    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 роки тому +1

    great video! The „mistakes“ make it all the more exciting and instructive. I would like to have your skills

  •  3 роки тому +1

    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 роки тому +3

    This channel is just great! I really appreciate your tutorials, sir!

  • @poplach
    @poplach 3 роки тому +4

    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.

  • @LaravelJutsu
    @LaravelJutsu 3 роки тому +2

    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 роки тому +1

      Maybe it is suitable too, you can try it. The relationships weren't the point of this video, so I didn't emphasize it.

  • @J87NL
    @J87NL 3 роки тому +4

    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 роки тому +1

      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 ;)

  • @thelostrider1
    @thelostrider1 3 роки тому +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 роки тому +4

      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 роки тому +1

      @@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.

  • @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 роки тому +1

      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.

  • @abdul-rehman
    @abdul-rehman 3 роки тому +2

    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 роки тому +1

      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 роки тому +1

      ​@@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 роки тому +1

      @@LaravelDaily did you uploaded any video or course about Laravel Jobs and Queues on the production server?

  • @GergelyCsermely
    @GergelyCsermely 3 роки тому +5

    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 роки тому +2

      Yes you're probably right, Sorrawut pointed out the same thing in another comment.

  • @MJacksi
    @MJacksi 2 роки тому

    Thank you!

  • @陳哲治-j8u
    @陳哲治-j8u 3 роки тому

    Thanks for your video.
    But in the video it seems that you can create an empty order without any exception?

    • @LaravelDaily
      @LaravelDaily  3 роки тому +1

      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.

  • @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?

  • @bboydarknesz
    @bboydarknesz 3 роки тому +1

    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 роки тому +1

      Livewire renders content on a server-side, then just replaces HTML with JavaScript

  • @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

  • @renwar
    @renwar 3 роки тому

    Awesome! Thanks

  • @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 роки тому +2

      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.

  • @msdeav
    @msdeav 2 роки тому

    So we basically need to use try-catch every time?

  • @hassanfazeel2354
    @hassanfazeel2354 3 роки тому

    Awesome tips!

  • @hariomnagar5606
    @hariomnagar5606 3 роки тому

    Thank you so much 💐💐

  • @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.

  • @ChangeYourLifeForever
    @ChangeYourLifeForever Рік тому

    awsm

  • @abdulwadoodkhan826
    @abdulwadoodkhan826 2 роки тому

    How to display cart items in table using livewire??

  • @carlosaires2688
    @carlosaires2688 3 роки тому

    This IDE is PHPStorm? If are, whats the theme it is. Thank you.

  • @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.

  • @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.

  • @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 роки тому +1

      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.

  • @ahmerayaz8825
    @ahmerayaz8825 11 місяців тому

    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();

  • @alisaprima2142
    @alisaprima2142 2 роки тому

    Hello Sir. I have a question. Is storing cart data in database better than storing data in session?

  • @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.

  • @nekozu_
    @nekozu_ 3 роки тому +1

    Create a video about design pattern in Laravel framework please

    • @LaravelDaily
      @LaravelDaily  3 роки тому +2

      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: ua-cam.com/channels/TuplgOBi6tJIlesIboymGA.htmlsearch?query=service

  • @rahman_abdu
    @rahman_abdu 3 роки тому

    how to add progress bar while importing excel

  • @nftsparatodos1274
    @nftsparatodos1274 3 роки тому

    Nice videos every day, are you thinking in create new courses in your teachable page ?

    • @LaravelDaily
      @LaravelDaily  3 роки тому +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

  • @bidhanbaniya7605
    @bidhanbaniya7605 3 роки тому

    How to dynamically switch the database in laravel hoping for your reply? 🥺

    • @ward7576
      @ward7576 3 роки тому +4

      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!

  • @ibrahimkhalaf4656
    @ibrahimkhalaf4656 3 роки тому

    Please I need your help in a task to get a job the task is about appointments

  • @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 роки тому +1

      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.

  • @YourMaster000
    @YourMaster000 3 роки тому

    Hey I like your video can u make a tutorial for running your project on .test

    • @LaravelDaily
      @LaravelDaily  3 роки тому +2

      For that, I just use Laravel Valet locally, and it does it automatically for me

    • @YourMaster000
      @YourMaster000 3 роки тому

      @@LaravelDaily thanks

  • @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.