Blade memungkinkan untuk menentukan arahan kustom kita sendiri menggunakan metode direktif. Ketika compiler Blade bertemu dengan direktif kustom, ia akan memanggil callback yang disediakan dengan ekspresi yang berisi arahan. Pada catatan kali ini akan dibuat arahan @currency($expression) yang memformat variabel tertentu, yang akan merubah angka menjadi format currency.
buka file AppServiceProvider.php dan masukan kode berikut
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace App\Providers; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function boot() { Blade::directive('currency', function ($expression) { return "Rp. <?php echo number_format($expression, 0, ',', '.'); ?>"; }); } public function register() { // } } |
kemudian panggil arahan tersebut pada view untuk digunakan seperti kode berikut ini
1 |
@currency($value->price) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@extends('layouts.app') @section('content') @include('widgets.alert-message') <div class="container-fluid"> <div class="row justify-content-center"> <div class="col-md-12"> <div class="card"> <div class="card-header"><a href="/admin/product/create" class="btn btn-primary">Add Product</a></div> <div class="card-body"> <table class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">Name</th> <th scope="col">Description</th> <th scope="col">Price</th> <th scope="col">Stock</th> <th scope="col">Image</th> <th scope="col">EDIT</th> <th scope="col">DELETE</th> </tr> </thead> <tbody> @foreach($product as $value) <tr> <th scope="row">{{ $value->id }}</th> <td>{{ $value->name }}</td> <td>{{ $value->description }}</td> <td>@currency($value->price)</td> <td>{{ $value->stock }}</td> <td><img alt="{{$value->foto}}" src="/img/products/{{$value->foto}}" width="50px" class="img-circle img-responsive"></td> <td><a class="btn btn-small btn-info" href="/admin/product/{{$value->id}}/edit">EDIT</a></td> <td><form method="POST" action="/admin/product/{{$value->id}}" class="pull-right"> {!! csrf_field() !!} <input name="_method" type="hidden" value="DELETE"> <button class="btn btn-small btn-danger" type="submit" value="Delete" onclick="if (!confirm('are you sure?')) {return false;};">DELETE</button> </form></td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> @endsection |