Download the PHP package mrnewport/laravel-priceable without Composer
On this page you can find all versions of the php package mrnewport/laravel-priceable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mrnewport/laravel-priceable
More information about mrnewport/laravel-priceable
Files in mrnewport/laravel-priceable
Package laravel-priceable
Short Description Quantity-based pricing with multi-tenant and multi-scope support for any Eloquent model.
License MIT
Informations about the package laravel-priceable
mrnewport/laravel-priceable
mrnewport/laravel-priceable is a Laravel package that provides quantity-based, multi-scope pricing for any Eloquent model. It allows you to define tiered pricing, attach custom price lists to specific users, and apply additional “scopes” (like region or channel) without the need for external dependencies.
This README walks you through installation, configuration, usage, advanced features, and testing.
Table of Contents
- Introduction
- Key Features
- Requirements
- Installation
- Configuration
- Migrations
- Usage
- Attaching the
Priceable
Trait - Defining Tiered Pricing
- Price Lists & User-Specific Pricing
- Scopes (Region, Channel, etc.)
- Retrieving a Price
- Attaching the
- Fallback Logic & Exception Handling
- Advanced: Date-Range Pricing
- Testing
- License
Introduction
MrNewport/LaravelPriceable
offers a complete and robust solution for dynamic pricing in your Laravel application. By simply adding a trait to any Eloquent model, you can define:
- Tiered prices based on quantity ranges.
- Multiple price lists for different groups (e.g., corporate, VIP, or partner).
- Scopes for region-, channel-, or any other custom-based pricing.
- Optional fallback to a default price if no specialized pricing is found.
- Date-range validations for time-limited pricing or promotions.
Everything is self-contained, including the test suite, which uses an in-memory SQLite database and does not rely on your host application’s models or migrations.
Key Features
- Tiered / Quantity-Based Pricing: Price changes automatically when buyers hit certain quantity thresholds.
- Multi-Tenant Support: Assign price lists to specific users or entire groups.
- Scopes: Add region, channel, or any additional dimension to fine-tune pricing.
- Date-Based Validity: Start and end dates for promotional or seasonal pricing.
- Fallback & Exceptions: Gracefully handle missing specialized prices.
- Polymorphic: Attach pricing to any Eloquent model with a single trait.
- Self-Contained Tests: No external references; everything is tested from within the package.
Requirements
- PHP: ^8.0
- Laravel: ^9.0 or ^10.0
- Database: Any supported by Laravel (tested primarily on MySQL & SQLite).
Installation
-
Require via Composer:
-
Optional: Publish the package config and migrations into your Laravel app (if you want to customize them in your project):
-
Run Migrations (if you published them or are auto-loading them):
- Configure (Optional): If you want the package to auto-load its migrations instead of publishing them, set
auto_load_migrations
totrue
inconfig/priceable.php
.
Configuration
The package ships with a configuration file located at:
When published, it appears in your Laravel app’s config/priceable.php
. Key settings include:
auto_load_migrations
: Iftrue
, migrations insidesrc/database/migrations
will be auto-loaded by the package.fallback_to_default_price
: When a user’s price list doesn’t match, fallback to the “default” price (whereprice_list_id
isnull
).throw_exception_if_no_price_found
: If no price is found (even after fallback), throw aPriceNotFoundException
instead of returningnull
.default_currency
: The default currency used in your price records.
Migrations
All migration files reside in:
If auto_load_migrations
is set to false
(default), you should publish and run them from your main Laravel application. Alternatively, set auto_load_migrations
to true
to have them loaded directly from this package.
These migrations create:
- price_lists: Names and descriptions for your custom or default price lists (e.g., “VIP”, “Corporate”).
- price_list_user: A pivot table linking users to price lists.
- prices: Stores tier-based pricing, including quantity ranges, currency, and optional date ranges.
- price_scopes: Key-value pairs (e.g., region=US, channel=B2B) for more granular pricing logic.
Usage
Attaching the Priceable
Trait
Any Eloquent model you wish to have dynamic pricing must use the Priceable
trait:
This adds a polymorphic relationship to the prices
table, letting you define multiple tiered prices for each product (or any other model).
Defining Tiered Pricing
You can define multiple quantity ranges by creating records in the prices
relationship:
If someone purchases 5 units, the price is $100
; for 15 units, $80
.
Price Lists & User-Specific Pricing
If you want specialized pricing for certain users:
-
Create a Price List:
-
Attach a user to that list:
- Add special prices under that price list:
When you call $product->priceFor(5, $user)
, the package checks the user’s VIP
price list first, and if found, uses $60
.
Scopes (Region, Channel, etc.)
You can attach key-value scopes to any price. For instance, define a region=US
or channel=B2B
scope:
When retrieving the price, pass scopes as an associative array:
Only prices containing all matching scopes (region=US
) will be considered. If none match, it falls back as configured.
Retrieving a Price
To get a final price, call priceFor($quantity, $user, $scopes = [])
:
Alternatively, you can use:
-
Facade:
- Helper:
Fallback Logic & Exception Handling
If a user has no custom price (or no matching scope), the system can fallback to a default price (price_list_id = null
). You can control this behavior via config:
fallback_to_default_price
:true
orfalse
.throw_exception_if_no_price_found
: If set totrue
, throws aPriceNotFoundException
if no price is found after fallback.
This ensures you never end up with an undefined or zero price unless you explicitly allow it.
Advanced: Date-Range Pricing
Each price record can optionally have valid_from
and valid_to
fields:
If the current date/time is outside that range, the system ignores this price.
Testing
The tests are completely self-contained and use in-memory SQLite. No external references to your application’s models or factories.
To run tests:
How It Works
- The package includes its own
tests
directory, with aTestCase
that sets up a test database in memory. - Minimal “test models” (
ProductTestModel
,UserTestModel
) are defined, along with tables for them. - All package migrations (
src/database/migrations
) are loaded to test every feature.
This ensures the package is thoroughly verified in isolation.
License
This package is open-sourced software licensed under the MIT license.
Enjoy using MrNewport/LaravelPriceable for your advanced pricing needs!