Creating Helper Function in Laravel 6 Example

Today, We want to share with you Creating Helper Function in Laravel 6.In this post we will show you Laravel 6 Create Custom Helper Function, hear for Creating Your Own PHP Helpers in a Laravel Project we will give you demo and example for implement.In this post, we will learn about how to call helper function in laravel blade with an example.

Creating Helper Function in Laravel 6

There are the Following The simple About Helpers – Laravel – The PHP Framework For Web Artisans Full Information With Example and source code.

  Class HTML FORM not found in Laravel 5 Error Solution

As I will cover this Post with live Working example to develop Best Practices for Custom Helpers in Laravel 6, so the Creating Your Own PHP Helper Functions In Laravel 6 is used for this example is following below.

Step 1: Create a Helpers

app\Helpers.php

<?php
use Carbon\Carbon;
if (!function_exists('expire_time_zone')) {
    function expire_time_zone(string $date)
    {
        return Carbon::parse($date, config('app.expire_time_zone'))->format('Y-m-d\TH:i:s');
    }
}

if (!function_exists('expire_time_zone')) {
    function getConverThumbImage($img_url) {
        $base_url = config('app.url');
        if (trim($img_url) == "") {
            return $base_url . '/api/uploads/default-150x150.png';
        }
        $file_img = pathinfo($img_url);
        if (is_array($file_img)) {
            return $file_img['dirname'] . '/' . $file_img['filename'] . '-400x400.' . $file_img['extension'];
        } else {
            return $img_url;
        }
    }
}

Step 2: create a Laravel Service Provider

MyAppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MyAppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        require_once app_path() . '/Helpers.php';
    }
}

Step 3: Register your Service Provider

config/app.php

    'providers' => [
       ...
        App\Providers\MyAppServiceProvider::class,
   ],

Web Programming Tutorials Example with Demo

Read :

  PHP String Cleanup Functions with Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about create helper function in laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of