Friday, December 27, 2024
Google search engine
HomeLanguagesLaravel 10 Create and Use Helper Function Example Tutorial

Laravel 10 Create and Use Helper Function Example Tutorial

If you need to use some code repeatedly in your Laravel applications. So for this, you do not need to write and use code again and again on controller, model, and blade views. For this, you can create a custom helper function in Laravel applications. With the help of which you will not need to write a code again and again and you will be able to use this code again and again on models, controllers, and views.

In this tutorial, you will learn how to create and use a custom helper function in Laravel 10 apps on blade view, controller, and model files.

Laravel 10 Create and Use Helper Function Example Tutorial

By using the following steps, you can create and use a custom helper function in Laravel 10 apps on blade view, controller, and model files.

  • Step 1: Create helpers.php File
  • Step 2: Setup File Path In composer.json File
  • Step 3: Execute Command for autoloading
  • Step 4: Use the helper function in laravel controller, model and blade view

Step 1: Create helpers.php File

In this step, you need create helpers.php in the laravel project inside the app directory.

In this file, you can write our own custom functions and call anywhere in your laravel blade view, controller and model file.

For example, you can create a following functions in your custom helpers.php file:

<?php
 
  function random_code(){

    return rand(1111, 9999);
  }


  function allUpper($str){
    return strtoupper($str);
  }

Step 2: Setup File Path In composer.json File

In this second step, you will add the path of the helpers file in the composer.json file. Let’s go to project root directory and open composer.json file and update the below-given code into the file:

composer.json

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
           "files": [

            "app/helpers.php"

        ]
    },

Step 3: Execute Command for autoloading

In this final step, go to command prompt and type the given command:

composer dump-autoload

After run the above command on command prompt. Then you can use custom helper functions by calling this functions name.

Step 4: Use the helper function in laravel controller, model and blade view

Once you have created a helper in laravel. Now, you will learn how to call or use above-created custom helper function in Laravel 10 by examples:

1 – How to call helper function in laravel blade

You can see the following example of how to call helper function in laravel blade view file:

<h2><?php echo allUpper('I am from geeksforgeeks.org') ?></h2>

2 – How to call helper function in laravel controller

You can see the following example of how to call helper function in laravel controller file:

    public function index()
    {   
        $data['title'] = toUpper('Title');
        return view('view', $data);
    }

Conclusion

In this tutorial, you have learned how to create helper and functions in Laravel 10. And as well as how to use/call helper functions in Laravel 10 on blade view, controller file.

Recommended Laravel Posts

RELATED ARTICLES

Most Popular

Recent Comments