Skip to content

Instantly share code, notes, and snippets.

@permatis
Last active November 25, 2019 02:37
Show Gist options
  • Save permatis/c6dac61570bfd34336f6 to your computer and use it in GitHub Desktop.
Save permatis/c6dac61570bfd34336f6 to your computer and use it in GitHub Desktop.
Membuat helper tanggal Indonesia di laravel 5 dengan Carbon.

Membuat Helper di Laravel 5

Berikut ini adalah langkah-langkah membuat helper di laravel 5 dengan contoh kasus membuat helper tanggal / datetime ke Indonesia.

  • Buat folder Helpers atau lainnya didalam folder App.

  • Membuat file Service Provider terlebih dahulu. gunakan perintah php artisan make:provider HelperServiceProvider

  • Kemudian buka file HelperServiceProvider.php di dalam folder App/Providers/

  • Tambahkan kode dibawah ini didalam fungsi register()

    foreach (glob(app_path().'/Helpers/*.php') as $filename){
    	require_once($filename);
    }
  • Kemudian tambahkan provider di file config/app.php

     //file app.php
     'providers' => [
     	'App\Providers\HelperServiceProvider',
     ],
  • Dan terakhir buat file helper didalam folder App/Helpers. Misal file Tanggal.php, silahkan lihat contohnya.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
<?php
namespace App\Helpers;
use Carbon\Carbon;
class Tanggal
{
public static function keIndonesia($tgl)
{
$dt = new Carbon($tgl);
setlocale(LC_TIME, 'IND');
return $dt->formatLocalized('%d %B %Y %H:%M:%S');
}
}
@teramuza
Copy link

cara manggilnya berarti gimana gan?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment