Belajar Laravel - Perhitungan Sederhana Laravel Part 3

Pada pertemuan kali ini kita akan membuat sebuah aplikasi sederhana menggunakan Laravel, beda dari sebelumnya. Sekarang kita akan membuat user dapat menginput "Kode Diskon" berdasarkan kode yang diberikan
  • Pertama Buka folder Routes > web.php
  • Isi file web.php dengan
use App\Http\Controllers\CalculationController;
Route::get('/', [CalculationController::class, 'index']);
Route::post('/', [CalculationController::class, 'calculate']);
  • Kemudian buat Controller baru, ketikkan di terminal perintah "php artisan make:controller CalculationController"


  • Ketikkan sintaks berikut kedalam "CalculationController.php"
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CalculationController extends Controller
{
    public function index()
    {
        return view('calculation');
    }

    public function calculate(Request $request)
    {
        $firstNumber = $request->input('first_number');
        $operation = $request->input('operation');
        $secondNumber = $request->input('second_number');
        $discountCode = strtoupper($request->input('discount'));
        $result = null;

        switch ($operation) {
            case 'add':
                $result = $firstNumber + $secondNumber;
                break;
            case 'subtract':
                $result = $firstNumber - $secondNumber;
                break;
            case 'multiply':
                $result = $firstNumber * $secondNumber;
                break;
            case 'divide':
                $result = $firstNumber / $secondNumber;
                break;
        }

        // periksa kode diskon yang diinputkan dan berikan diskon sesuai
        switch ($discountCode) {
            case 'SMKN1':
                $result *= 0.8; // diskon 50%+30%
                break;
            case '':
                // jika kode diskon tidak diisi, jangan memberikan diskon
                break;
            default:
                // jika kode diskon tidak valid, tampilkan pesan error
                return view('calculation', ['errorMessage' => 'Kode Diskon tidak valid']);
        }

        return view('calculation', ['result' => $result]);
    }
}
  • Buat file "calculation.blade.php" di dalam folder views, yang berisi sintaks berikut
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Perhitungan Sederhana</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
</head>

<body>

    <div class="container mt-3 d-flex justify-content-center">
        <div class="card align-center justify-content-center align-items-center d-flex col-md-6">
            <div class="card-body">
                <h1 class="text-center mt-1 mb-3" style="font-family: Poppins">Perhitungan Sederhana</h1>
                <hr>
                <form class="py-3" method="POST">
                    @csrf
                    <label for="first_number" class="mb-2">Angka Pertama:</label>
                    <input type="number" class="form-control" id="first_number" name="first_number" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;" required><br>
                    <label for="operation" class="mb-2">Operasi:</label>
                    <select id="operation" class="form-control form-select" name="operation" required>
                        <option value="">-- Pilih Operasi Aritmatika --</option>
                        <option value="add">Penjumlahan (+)</option>
                        <option value="subtract">Pengurangan (-)</option>
                        <option value="multiply">Perkalian (*)</option>
                        <option value="divide">Pembagian (/)</option>
                    </select><br>

                    <label for="second_number" class="mb-2">Angka Kedua:</label>
                    <input type="number" class="form-control" id="second_number" name="second_number" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;" required><br>


                    <label for="discount" class="mb-2">Discount Code: <i><small>(Optional)</small></i></label>
                    @if (isset($errorMessage))
                        <p style="color: red;">{{ $errorMessage }}</p>
                    @endif
                    <input type="text" class="form-control" id="discount" name="discount"><br>
                    <p>Special discount codes:</p>
                    <ul>
                        <li><span class="badge text-bg-danger">SMKN1</span> 50% + 30% discount</li>
                    </ul>


                    <button type="submit" class="btn btn-primary col-12">Calculate</button>
                </form>
            </div>

            @if (isset($result))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    Result: {{ $result }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>
  • Jika berhasil akan tampil di browser, seperti ini

Posting Komentar

Lebih baru Lebih lama

POST ADS1

POST ADS 2