[Live-Coding] Refactor Laravel Controller to be Much Shorter

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ •

  • @MimisK88
    @MimisK88 4 роки тому +130

    In a 15-minute video, I learned more than in 2 hours lessons! Thank you very much!

  • @maywhen-lusian
    @maywhen-lusian 5 років тому +32

    I was looking for channel like this for so long, till now I haven't found a dev which is advanced and also shares his knowledge of architecture beyond controllers

  • @bulent2435
    @bulent2435 4 роки тому +5

    This is the best Laravel video I've ever seen.

  • @arslanramay
    @arslanramay 4 роки тому +14

    This is the best 15 minutes video I have ever watched. Amazing tips.

  • @blank001
    @blank001 4 роки тому +1

    This video is 100% informative and 80% eyes hurting.

  • @zubayerhossain7074
    @zubayerhossain7074 4 роки тому +15

    Beyond pro level, watched that video before going to sleep. Now its 4:44 am I'm still watching it again and again , learning every topic that showed!
    Thanks man!

  •  Рік тому +2

    This was amazing. I never had seem so soft refactoring
    Thanks and again: you're the best

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

    idk, youtube recommends these with some delay :D I just started doing the same thing with my huge controllers at work, and now I see this in my feed :D How cool :)

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

    Phenomenal! I have a good 3-4 hours of reading and experimentation to do after watching this.

  • @theancientpersian
    @theancientpersian 2 роки тому +1

    In a 15 min video, I learned more than in 2 hours lessons! You're the best, Thank you very much!

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

    That's insane man, from many lines of codes to, two lines of code. That's incredible, thanks much for sharing this with us...i'm new in Laravel and this is my dream to code like this.

  • @Mutahar009
    @Mutahar009 2 роки тому +1

    This video has cleared so many concepts. not necessarily we apply all of these but so much information and practically displayed. Awesome Sir !

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

    How could I be so wrong for so long?
    Thanks for this...this is gold!

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

    Wow!!! The best refactory that I saw! Congrats!

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

    Brilliant... Looking forward to more of such refactoring videos.

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

    Thanks a lot for making these kind of videos. Recently discovered your channel and slowly watching your videos. It's amazing how you make subjects that were unknown to me very easily understandable and right away I can see the use cases for my own projects because you show real-life examples. An amazing channel and huge compliments to you as a teacher.

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

    Thank u so much sir for giving us your precious time . I started following you and you are so good.

  • @minawilliam11
    @minawilliam11 5 років тому +47

    Also it will be better to user request->validated() instead of request->all() to insert the validated fields only

    • @WhatAboutTechBaby
      @WhatAboutTechBaby 4 роки тому +1

      Can be considered but if you declared a fillable array on the model it won't assign anything other than those values declard in the model. But from readability you're right

    • @AnggaAriWijaya
      @AnggaAriWijaya 4 роки тому

      Yes, Somebody could inject html input and randomly guessing the another input that shouldn't update in that form (included in fillable variable)

    • @WhatAboutTechBaby
      @WhatAboutTechBaby 4 роки тому

      @@AnggaAriWijaya laravel will only fill values which (if) are specified in the fillables array on the model

    • @AnggaAriWijaya
      @AnggaAriWijaya 4 роки тому

      @Lukas Korbitsch I know that, assume you have model Customer with fillable fields: name, email, status, and some user allowed to update ONLY the status of customer (set active or inactive), in that form user could inject input name or email that Shouldn't updated

    • @WhatAboutTechBaby
      @WhatAboutTechBaby 4 роки тому

      @@AnggaAriWijaya ah I see where you going yeah you're right. But in this scenario I wouldnt build it like thst

  • @hohoho2180
    @hohoho2180 2 роки тому +5

    Reasonable for fat actions. If applied for small ones, now instead of quickly seeing all functionality at single look , you will have to navigate +5 or more extra files.

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

      Exactly, instead of 20 lines, now hundreds in many files, without any comment in controller.

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

    Wow this was so well done. Short and so much informative. Thanks

  • @hayyanebrahem5055
    @hayyanebrahem5055 5 років тому +1

    This is one of the best laravel lessons on youtube

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

    Thanks for explaining, was searching for this kind of video for a while. It all makes a bit more sense.

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

    i'm grateful that we have teacher like you, when i'm doing programming i study core php for 1year and i hate laravel so much but when i find job as programmer they required me to study laravel (the framework most i hated) i hate laravel because im also security researcher and i know laravel have zero day exploit but i have no choice i need to study laravel and one day youtube suggest your channel now im fully understand the laravel and what is advantages of laravel and now im started to love it thanks man! you save me to find awesome job now im junior developer i'm already 3months in job as junior programmer/developer but i think my knowledge is enough as senior developer thank you very much bro.
    but i have request im hard to understand and refactor the Policy/authorization the most important part of developing app, please help me to understand newbie like me the how to use policy and add custom method using authorizeResource() and resourceAbilityMap()
    Thanks bro

    • @Motassem.K
      @Motassem.K 3 роки тому

      I'm sorry but what do you mean by laravel has zero day exploit?
      and doesn't the exact same thing apply to every major open source language/framework ?

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

    Amazing video! I was searching for something like this for years. I always disliked most tutorials polluting the controller with "ifs" and "for" and validations.

  • @noorahmed5115
    @noorahmed5115 4 роки тому +15

    Thanks for the great tutorial. But I think it can sometimes make other developers worried if they are not following the same path. For example now the controller is very tiny. They are debugging an issue, they see that some data is being updated, but where is it? I have seen the observers for the first time here. Same may be the case with many people. This is indeed handy and keeps the code clean and short, but can sometimes create confusion. When it comes to FormRequests, yeah they are common, so if a developer does not find validation in Controller, they wiill directly think that yeah it can be in the FormRequest. So I am in 50 50 for using those observers and the like stuff. they can complicate things a little bit.

    • @MahmoudMohsab
      @MahmoudMohsab 3 роки тому +6

      a good solution would be to add a comment to inform the developers that that specific action is handled in an observer

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

      @@MahmoudMohsab yes you are right.

    • @MsMmaadd
      @MsMmaadd 2 роки тому +2

      @@MahmoudMohsab Thats exactly what I was asked to do when I submitted PR .

  • @sizulabs
    @sizulabs 4 роки тому

    Its realy a nice and comprehensive demonstration, learnt alot

  • @matejl92
    @matejl92 6 років тому +11

    What is the difference between observers and events? I would rather use event over observer because code in controller tells me what happens. It's kinda hidden when you do it with observers. What do you think?

    • @PovilasKorop
      @PovilasKorop 6 років тому +7

      Good point, I agree with you. Another thing is that observers are mostly for MODEL observing, specifically for created/updated etc, and events are broader and can be used anywhere.

    • @LeonardoMatosLopes
      @LeonardoMatosLopes 4 роки тому

      Good points of view, guys. I agree with all you said.

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

      Basically in simple terms you can call Observers as Model Hooks where we can hook some code before or after Create, Update, Delete kind of stuff.
      Where as events are like when something happened. So basically I would put the Admin Notifiations code in Events where we could listen for more stuff. So I would delegate my code here. Eg: PostPublished
      Jobs on the other hand is like before something happens Eg: PublishPost
      There lies a thin line which coud confuse most of the developers but over the time you will get to know after using in much of the projects.

  • @Tyche.Crypto
    @Tyche.Crypto 4 роки тому +4

    The MVC concept is to put all the logic functions model (such word counting), the controller to interact between the model and the view. This is my basic understanding of MVC.

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

    very good, I was impressed, I will take your course

  • @aberdeener
    @aberdeener 4 роки тому

    This was insanely helpful, thanks so much. Laravel blows me away everyday

  • @alila3883
    @alila3883 4 роки тому

    the best example you will see it the world!
    Thank you ..

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

    OMG! now it is two lines of code. Amazing!

  • @MrMacphist0
    @MrMacphist0 6 років тому +11

    great explanation! PS. As you also showed only([...]), you could've also used $request->validated() instead of ->all().

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

    Liked this video? I have many more: subscribe to my 30+ Laravel courses laraveldaily.com/courses

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

    Great video, I would also recommend using collection methods with anonymous function instead of foreach, for eg :
    $posts->each(function ($post) use ($otherStuff) {
    //Do here
    }
    You can also use &$parameter for sending your variable by reference and it will be saved after anonymous function, similar to pointer in C/C++

  • @alebuda117
    @alebuda117 4 роки тому

    Thanks a lot! Keep doing examples like this, really useful for me

  • @elanorriley8485
    @elanorriley8485 4 роки тому

    What's the use case between a service and adding a method like that to the Post model, for instance?

  • @odevperovano
    @odevperovano 4 роки тому

    Congratulations! You really bring valuable content in your videos!

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

    I really appreciate your work. kudos

  • @deeps8504
    @deeps8504 5 років тому

    Great example and nice explanation

  • @kevinflorenzdaus
    @kevinflorenzdaus 5 років тому

    Hope to see more Live Coding session from your channel ..

  • @Рефералывинтернете

    what if I want to update my post in other place but without sending the email?

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

    Perfect! Great video, I learned a lot.

  • @zoroXgamings
    @zoroXgamings 4 роки тому

    sir your videos are too gud and helpful , i even started your free course for beginners in laravel, and i m waiting if you will uplaod any course realted to vue and laravel api

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

    This was really helpful! Thank you.

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

    can i use a custom method name on my controller and have my observer listen to that method

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

    Потрясающе! Спасибо огромное :))))

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

    how to use redirectAction here?

  • @SaiyanJin85
    @SaiyanJin85 4 роки тому +18

    Although the code is clean there is something that bugging me and that is that it’s not apparent all these extra functionalities which makes debugging much harder. And I don’t mean in Laravel specifically. Unless some kind of mechanism has been set up like let’s say a plug-in system you are going to pull your hair out in a real world much complicated project

    • @SaiyanJin85
      @SaiyanJin85 4 роки тому +1

      @@davidbasil3161 In general comments should be used when there is a complicated piece of code otherwise code should be self explanatory through the name of the methods and attributes IMO. Also in my experience if you have a code base with lots of comments it's most likely that people won't pay much attention to them. If there are some comments here and there it's much more alarming to fellow devs when they see these comments

    •  2 роки тому

      @@SaiyanJin85 tru. In years my code became so semantic, so I can send a screenshot to my project owner who has no clue about programming asking him if the business logic is okay, and he is able to validate it :D

  • @DanielŚmigiela
    @DanielŚmigiela 3 роки тому

    the best video! You're the best, man !

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

    How to check for email and sms success and failure, whether its sent or failed throwing exceptions

  • @OpinionatedAnon
    @OpinionatedAnon 4 роки тому

    I don’t normally follow without good cause. But I’m following you.

  • @hamzac.4555
    @hamzac.4555 4 роки тому

    well, does it work for all the version ?

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

    You couldn't call observer in your postController, so how update method knows about observer?

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

    in 41st line of the original code, there is $words_count(), which i think should be $words_count. is it?

  • @randomguy5922
    @randomguy5922 4 роки тому

    i am using maatwebsite excel package, is there a way to import data and check validation on some specific fields, if any record fails to match validation, record the whole rows in a report excel file with the error message or red color in that field text which are not proper (the reason why validation failed) and then finally after full import have and option to download error excel file. So that we can correct the fields and upload that file again.Do you have any tutorial like this.
    I will be highly thankful if you guide me how to do this, i have tried to read the maatwebsite excel package documentation, but it is not very much clear to me.

  • @Paltibenlaish
    @Paltibenlaish 4 роки тому +1

    one question, if you are new to this kind of project how do you know, that there is a service and an observer if you don't reference it in the controller?

    • @PovilasKorop
      @PovilasKorop 4 роки тому

      Well, a lot of things may be outside of controllers, it's not only in the controllers you should look, you need to research all folders to understand how it is structured.

    • @Paltibenlaish
      @Paltibenlaish 4 роки тому

      ​@@PovilasKorop yes, im still a beginner in laravel, but what it is very practical to me is to follow the view controller model, clearly defined in the routes/web. I hope to get more pro soon, :)

    • @Shez-dc3fn
      @Shez-dc3fn 4 роки тому +1

      @@Paltibenlaish this isnt specific to laravel btw - other frameworks even languages you should aim to do coding like this..

  • @abhinavgupta2808
    @abhinavgupta2808 5 років тому

    You have great knowledge of Laravel..

  • @VitaliyGura
    @VitaliyGura 4 роки тому +32

    NOtifications can be Queueable, no need to make Job.

  • @Sunwarul
    @Sunwarul 5 років тому

    Thanks man. Very good examples. Make more videos please...

  • @kabinghimire1101
    @kabinghimire1101 5 років тому

    its great video and u explain so simply thanks man

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

    At 3:22, why not use request->validated() to pass only those fields which are being validated through request.

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

      this is an old video, he uses validated() nowadays because of obvious security flaws

  • @bernardonigbinde
    @bernardonigbinde 6 років тому +1

    Great Tutorial @Povilas!

  • @TheGajos
    @TheGajos 4 роки тому +1

    Awesome tutorial 🙏 Thanks a lot man!

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

    why start observer on app service provider instead of on post class?

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

      Just one of the options, personal preference

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

    omg this video so amazing tks you so much for share your knowledge

  • @Paltibenlaish
    @Paltibenlaish 4 роки тому

    hi guys, great material here.
    I need to make a functionality to take a csv as a request and update multiple tables,
    how would be the correct way to afront this in laravel,??
    (-the validation of the csv -creating records in many database and sending a response )

    • @PovilasKorop
      @PovilasKorop 4 роки тому

      There's no one "correct" way. It depends on the complexity of your CSV, what kind of validation you need, and what you need to do after validation errors or after creating records. On each of the steps, there may be an error and you need to handle it in your way. You won't get such an answer in a simple UA-cam comment.

    • @Paltibenlaish
      @Paltibenlaish 4 роки тому

      @@PovilasKorop haha, right, thank you for your answer, really looking forward for more of your amazing videos :)

  • @webhostingindia
    @webhostingindia 4 роки тому

    Nice work !😊

  • @alexanderdera1266
    @alexanderdera1266 6 років тому +2

    Thanks. It's a great tutorial!

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

    Awesome, very helpful

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

    Thanks as always.

  • @tadeubdev
    @tadeubdev 4 роки тому

    Thanks for this video! It's so helpful!

  • @ahmedhdeawy
    @ahmedhdeawy 4 роки тому

    Thanks for your effort

  • @boxexify
    @boxexify 5 років тому

    I like the way you do it!

  • @fajarsetiawansiagian
    @fajarsetiawansiagian 4 роки тому

    this video make me understand how to refactor my controller

  • @LeonardoMatosLopes
    @LeonardoMatosLopes 4 роки тому

    Awesome video! Thank you!

  • @veljkoveljkovic8410
    @veljkoveljkovic8410 4 роки тому +2

    magic method __LaravelBusiness thank you :-)

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

    WOW. Grate Tutorial ❤

  • @vaniulen84
    @vaniulen84 5 років тому

    Why you dont use post_id ? I get response methodNotAllowedHttpException with this code update(Post $post).

    • @LaravelDaily
      @LaravelDaily  5 років тому

      Maybe you're getting that because you're doing GET request instead of POST? In CRUD-based controllers, it's pretty convenient to pass model object, and avoid one more find() in controller, so it makes controller shorter and more readable. Read about model binding here in official docs: laravel.com/docs/5.7/routing#route-model-binding

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

    hello, lately I've been watching your videos a lot. many good things i've learn from you. thank you so much.
    but I have a question how to prevent race conditions when using observers or event listeners or services
    do you use lockForUpdate method? please make video about lockForUpdate, rollback related with observers, event listener, and services. thank you.

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

    Shouldn’t it be $request->validated() instead of $request->all() ??

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

    nice teaching.

  • @ajithlal1688
    @ajithlal1688 4 роки тому +1

    That was pretty cool ❤️❤️❤️

  • @herminioheredia
    @herminioheredia 5 років тому

    I think it was not necessary to use the Job with the notification, You can implement the ShoulQueue interface and Laravel takes care of the rest.

  • @AntonyKouzos
    @AntonyKouzos 4 роки тому

    This would be a great addition to QuickAdminPanel. Being able to export a solution as a module and add it to a project would help continuous development.

    • @PovilasKorop
      @PovilasKorop 4 роки тому

      It's almost impossible to export something as a module that would fit into any project, we just can't build modules that flexible, sorry.

    • @AntonyKouzos
      @AntonyKouzos 4 роки тому

      @@PovilasKorop It's already a great tool and your content is extremely helpful. Thank you for your answer.

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

    give me this code, is it in git?

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

    man,you are awesome!!!!!

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

    Great stuff, It would be better if you have automated tests and for each modification you do, run the tests.

  • @zubayerhossain7646
    @zubayerhossain7646 4 роки тому

    Outstanding!

  • @manoj.kiran.a
    @manoj.kiran.a 6 років тому

    Great tutorial

  • @ruwanbandara140
    @ruwanbandara140 4 роки тому

    Thank you so much. I learned lot

  • @neuroboost2585
    @neuroboost2585 5 років тому

    Nice and usefull tutoriall. Please some more info about clean and nice code. Does $request->all() automatically perform the validation??

    • @PovilasKorop
      @PovilasKorop 5 років тому

      No, $request->all() doesn't perform validation. Validation is in whatever FormRequest class you pass to the store() as parameter. Read more about FormRequest classes: laravel.com/docs/master/validation#form-request-validation

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

    Wonderfull job!

  • @nadeemahmed7947
    @nadeemahmed7947 4 роки тому

    Thank you so much very help full

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

    Thank you very much!

  • @a1tem
    @a1tem 5 років тому

    Thank you! Great example I do the same in my codebase!

  • @imannasr
    @imannasr 4 роки тому

    Hi and thanks for this video.
    It's great. Thanks a lot

  • @codeology1
    @codeology1 6 років тому

    Oh my god ... amazing tutorial

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

    You are great
    thanks alot

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

    Thank you.