Laravel Global Helper Functions: How to Use

Поделиться
HTML-код
  • Опубликовано: 21 окт 2024

Комментарии • 50

  • @konstruktion
    @konstruktion 3 года назад +10

    Please more "How to structure a Project"-Videos! Helper, Services and so on. It's not so easy. 🙏🏻

  • @kathireshpalanisamy
    @kathireshpalanisamy Год назад +1

    Straight to the point

  • @utmostcreator
    @utmostcreator 3 года назад +1

    I am building my framework work, and Laravel is the thing that inspires me!
    I am viewing the core implementation and all that kind of things, And when I had seen the app() or auth() for the first time, I was puzzled. How come?
    But I have found it on my own.
    Your tutorials are also very short, squeezed, extremely powerful, and all of them are worthwhile to watch!
    It would nice to see a crash course about Laravel 8, all new features, tips and tricks.
    Thanks!

  • @webpakistan9589
    @webpakistan9589 3 года назад +1

    Although I know this practice before I would say you are doing great job . Learned a lot from you . I was looking for kind of person who can teach best practices.

  • @jussymashunye
    @jussymashunye 3 года назад

    I cant thank you enough, again demystifying these concepts

  • @AaronCarpenter-LegendaryLion
    @AaronCarpenter-LegendaryLion 3 года назад +2

    Keep it up, Pavilos. These are amazing!

  • @francescoleto2823
    @francescoleto2823 3 года назад +1

    Great! I usually use a Trait for this. But now I'll change my practice. Thanks sir

    • @LaravelDaily
      @LaravelDaily  3 года назад +4

      Traits should be used when you want to re-use the same methods in a few classes. So if it follows that structure, Traits are better. But if it's flexible to be used wherever, then it's a helper.

  • @baraahjarkas5517
    @baraahjarkas5517 8 месяцев назад

    very useful, thank you

  • @ironspider9026
    @ironspider9026 3 года назад

    Nice and easy thanks for sharing !

  • @jniyaz
    @jniyaz 3 года назад +1

    Great, real-time useful update.. thanks man.

  • @sheryllapus4837
    @sheryllapus4837 Год назад

    this is working on my local, but on my production it isn't, it says "Call to undefined function", do you have any idea whats wrong? i already uploaded the compose.json also
    thanks

  • @RamzySaad
    @RamzySaad 6 месяцев назад

    Great work ❤

  • @Cod3rMax
    @Cod3rMax 2 года назад

    What i do if i have for example a function that i need to use it in all the project and has a lot of lines i just create a package for it :) and in the future some of the packages i make them public such as proxy grabber, proxy checker....etc

  • @OlegHolovko
    @OlegHolovko 3 года назад

    It's really helpfull, thank a lot!

  • @urufu
    @urufu 3 года назад

    hello I love your videos so much because you learn a lot. I have a question in livewire, how could I make 6 radio buttons with YES and NO questions, if I randomly select 3 in YES a button will be deactivated. Thank you

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      Sorry I can't answer that in a RUclips comment, you're asking to create a full Livewire component.

  • @moutihmarouane4698
    @moutihmarouane4698 3 года назад

    you can also do it in serviceProvider
    public function register()
    {
    foreach (glob(__DIR__.'/../Helpers/Functions/*.php') as $filename) {
    require_once $filename;
    }
    }

  • @Laracoding
    @Laracoding 3 года назад

    You are an awesome man ,,, thanks

  • @binodthakur6113
    @binodthakur6113 3 года назад

    Great sir, Always love the content

  • @iammuttaqi
    @iammuttaqi 3 года назад

    It makes problem on the live server sometimes..

  • @vitouvitou5269
    @vitouvitou5269 3 года назад

    Thank you teacher

  • @phpcoding9282
    @phpcoding9282 3 года назад

    I had code rejected for using a helper file with some functions in it, I was told I should be using ServiceProviders, what are your thoughts on this?

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      Depending on the coding standards of that company. I kind of agree that helpers should be used as a last last resort, better to wrap them in some kind of class by topic, like DateHelper or something.
      Cause global helpers are partly a bad pattern if all other system is OOP.

  • @lakatosgabor5410
    @lakatosgabor5410 3 года назад

    Hi!
    How can i use the laravel helpers in JS file? For example, I have a periods.js file, and one show_helper() function. How to call the helpers function in JS function?
    Thanks your help!
    Gabor

  • @bhavyakumarpanchotiya8254
    @bhavyakumarpanchotiya8254 2 года назад

    so so so goooooooooooooood

  • @rafamorawiec
    @rafamorawiec 3 года назад

    But what if you have one helper and someone else create helper with the same name and you use function_exists() but you need that your helper should be executed? It won't get better without function_exists when you will get error and you can make suitable changes?

  • @rehmanghani6726
    @rehmanghani6726 Год назад

    Nice Video Povilos.
    Can you Create a video about the Laravel Facade vs Laravel Helper class?
    We could create both Facade and Helper class and use them as a utility in our project.
    so what is the difference and when we should Create Facade or Helper class?
    Thank you

    • @LaravelDaily
      @LaravelDaily  Год назад

      I don't actively use Facades, so I can't shoot a direct comparison video. I prefer Helpers.

    • @rehmanghani6726
      @rehmanghani6726 Год назад

      @@LaravelDaily OK Thank you!

  • @Perffy
    @Perffy 3 года назад

    i am interested in one thing. why i cannot place a redirect() method in a helper function? is there any way to achieve this?

    • @LaravelDaily
      @LaravelDaily  3 года назад

      You cannot? What is the error you're getting?

    • @Perffy
      @Perffy 3 года назад

      @@LaravelDaily i dont get any errors. If i call return redirect() inside a helper function it doesn’t happen anything. If i call echo redirect() it works, but than for a second i see a screen with some header text

    • @LaravelDaily
      @LaravelDaily  3 года назад

      Then there's something wrong with how your helper is written/called, needs debugging, can't do that in a RUclips comment.

  • @MrFxJ
    @MrFxJ 3 года назад

    Thank you. One question though, does this affect app performance since all functions in the file get auto loaded and app checks if no other function with the same name exists ?

    • @LaravelDaily
      @LaravelDaily  3 года назад

      The effect is really minimal, in my opinion.

    • @marcincook
      @marcincook 3 года назад

      Create class, maybe good name is Helper and call static like Helper::user() Helper::motivation() etc...

    • @utmostcreator
      @utmostcreator 3 года назад +1

      I believe it is similar to other loaders. e. g. webpack, the fewer file your are loading the better performance.
      So, yeah, one file shouldn't be an issue! if it saves you a whole lot time!

  • @Towzlie
    @Towzlie 3 года назад

    Good ! Thanks !

  • @dhanrajchakravarti1368
    @dhanrajchakravarti1368 3 года назад

    Can we call a controller function without declare route using ajax calling
    how can i add rich text editor in livewire components

    • @LaravelDaily
      @LaravelDaily  3 года назад

      No you can't without route
      Text editor CKEditor example here: livewirekit.com/components/livewire-ckeditor-form

  • @IlPandax
    @IlPandax 3 года назад

    Thank you for the video. Can you create a video about facades and how to create a custom one? Thanks!

    • @LaravelDaily
      @LaravelDaily  3 года назад

      I've never needed to create Facades manually in my projects.

  • @amgadalwattar2863
    @amgadalwattar2863 2 года назад

    thank youu

  • @satashanofficial
    @satashanofficial 3 года назад

    Hi Povilas, this really very helpful. I'll use this.
    I have quest if you please describe this which practice is best.
    in the below class when a request comes into the controller I store the request in a variable then everywhere I can use it.
    class abcController {
    private $request;
    public function __construct(Request $request)
    {
    $this->request = $request;
    }
    }
    in the below class when a request comes into the controller I pass the request to a related function so ill use it within the function.
    class abc {
    public function getAll(Request $request)
    {

    }
    }
    please share your opinion

  • @KamranBhutto
    @KamranBhutto 2 года назад

    I face problem Division by zero

  • @Jurigag
    @Jurigag 3 года назад

    Well i will be honest - i would never use something like this. If you need some global function without any context then maybe you are doing something wrong? For me all functions should be methods of the class.

    • @jagunghensem7260
      @jagunghensem7260 3 года назад

      for example you'd have a number like 9999.99 and in some cases you want it to be in money form like USD 9,999.99 and sometimes just 10000 (no comma) like in quantity or just want to ignore the .99 at the end to make just 9999. I think this is the perfect scenario to use these kind of functions because
      1) you can put parameters in it like display_numbers($number = 9999.99, $decimal_places = 2, $allow_rounding = true)
      2) you can literally use it in blade or controller or model, etc...
      3) its more readable like.. doing number_format(floatval($number), 2) can be quite confusing sometimes when there's already a lot of codes to look at
      this is one example of course~
      another one that I usually use is to parse and display date because I wouldn't want to put \Carbon\Carbon::parse($resource->approved_date)->format('d/m/Y') in everywhere - its long and what if resource is null, so you'd want to make some checks.. well just my two cents