laravel-discount maintained by ekramhossain
Laravel Discount Calculator
A simple and lightweight Laravel package to calculate discount percentage between original price and selling price.
This package provides a clean Facade-based API for calculating discounts easily inside Laravel applications.
Features
- Calculate discount percentage
- Simple Facade usage
- Laravel Service Provider integration
- PSR-4 Autoloading
- Lightweight and easy to use
Requirements
- PHP >= 8.1
- Laravel 9.x
- Laravel 10.x
- Laravel 11.x
- Laravel 12.x
Installation
Install the package via Composer:
composer require ekram/laravel-discount
Usage
Import the Facade:
use Ekram\Discount\Facades\Discount;
Calculate discount percentage:
$discount = Discount::calculate(280, 250);
echo $discount;
Output:
10.71
How It Works
Formula:
Discount Amount = Original Price - Selling Price
Discount Percentage =
(Discount Amount / Original Price) × 100
Example:
Original Price = 280
Selling Price = 250
Discount Amount = 30
(30 / 280) × 100
= 10.71%
Controller Example
<?php
namespace App\Http\Controllers;
use Ekram\Discount\Facades\Discount;
class DiscountController extends Controller
{
public function calculate()
{
$originalPrice = 280;
$sellingPrice = 250;
$discount = Discount::calculate(
$originalPrice,
$sellingPrice
);
return response()->json([
'original_price' => $originalPrice,
'selling_price' => $sellingPrice,
'discount_percentage' => $discount . '%'
]);
}
}
Response:
{
"original_price": 280,
"selling_price": 250,
"discount_percentage": "10.71%"
}
Facade
This package provides a simple Facade:
Discount::calculate($originalPrice, $sellingPrice);
Internally it resolves the Discount service from Laravel Service Container.
Service Container Binding
The package registers:
'discount'
inside Laravel Service Container.
Example:
app('discount')->calculate(280,250);
Validation
The package automatically handles invalid original price.
Example:
Discount::calculate(0, 100);
Returns:
0
Package Structure
laravel-discount/
├── src/
│ ├── Discount.php
│ ├── DiscountServiceProvider.php
│ └── Facades/
│ └── Discount.php
│
├── composer.json
└── README.md
Author
Md. Ekram Hossain
License
This package is open-sourced software licensed under the MIT license.