Downloads
Stars
Version
This package allows you to manage your pricelists and reorder items inside. It's not a kind of pricing tables, where you have a list of features and a price below. It is responsible for creating lists, where every item has its own price. Also every item can belong to many pricelists.
This package includes:
You can install the package via composer:
composer require nestermaks/laravel-pricelist
Publish and run the migrations with:
php artisan vendor:publish --tag=pricelist-migrations
php artisan migrate
You can override translation files of add a new one. Publish lang files with:
php artisan vendor:publish --tag=pricelist-translations
You can override the default options for used locales, api route prefixes and validation rules. Publish the configuration:
php artisan vendor:publish --tag=pricelist-config
This is the contents of the published config file:
<?php
// config for Laravel Pricelist
return [
/////////////////////////////////////////////
//Locales for Astrotomic/laravel-translatable
/////////////////////////////////////////////
'locales' => [
'en',
'ru',
'uk',
],
///////////////////
//API routes prefix
///////////////////
'api' => 'nestermaks-api',
//////////////////////////
//Pricelists routes prefix
//////////////////////////
'pricelists' => 'pricelists',
///////////////////////////////
//Pricelist items routes prefix
///////////////////////////////
'items' => 'pricelist-items',
////////////////////////////////////////////////
//Items amount shown when index method is called
////////////////////////////////////////////////
'pricelists-per-page' => 10,
'pricelist-items-per-page' => 10,
///////////////////////////////////////////////
//Validation rules for store and update methods
///////////////////////////////////////////////
'store-pricelists' => [
'title' => ['required', 'max:256'],
'description' => ['max:1000'],
'lang' => ['max:16'],
'order' => ['numeric', 'min:0', 'max:65535'],
'active' => ['boolean']
],
'update-pricelists' => [
'title' => ['max:256'],
'description' => ['max:1000'],
'lang' => ['max:16'],
'order' => ['numeric', 'min:0', 'max:65535'],
'active' => ['boolean']
],
'store-pricelist-items' => [
'title' => ['required', 'max:256'],
'units' => ['required', 'max:256'],
'lang' => ['max:16'],
'shortcut' => ['required', 'max:256'],
'price' => ['required', 'numeric', 'min:0'],
'max_price' => ['prohibited_if:price_from,true'],
'price_from' => ['boolean'],
'active' => ['boolean']
],
'update-pricelist-items' => [
'title' => ['max:256'],
'units' => ['max:256'],
'lang' => ['max:16'],
'shortcut' => ['max:256'],
'price' => ['numeric', 'min:0'],
'max_price' => ['prohibited_if:price_from,true'],
'price_from' => ['boolean'],
'active' => ['boolean']
],
];
Migrations files are provided with such tables:
pricelists. This table is like a container for pricelist items. It has such fields:
order
- sets priority on a frontendactive
- defines if the pricelist is active or notTranslatable fields of the table 'pricelists' are in the table 'pricelist_translations'. Table 'pricelists' has many pricelist translations
pricelist_translations. Fields:
pricelist_id
- id of the related pricelistlocale
- language of translationtitle
- title of the pricelistdescription
- description of the pricelist if neededpricelist_items. Elements of pricelists. Fields:
shortcut
- for internal usage to identify your pricelist item. For example, if you have different items with the same titleprice
- price of your product. In case the 'max_price' field is set, this one stands for minimum price.max_price
- if your product has no fixed price this field describes maximum price.price_from
- if the price of your product starts from the value set in 'price' field and has no upper bound. Boolean.active
- defines if the pricelist item is active or notTranslatable fields of the table 'pricelist_items' are in the table 'pricelist_items_translations'. Table 'pricelists' has many pricelist translations
pricelist_items_translations. Fields:
pricelist_item_id
- id of the related pricelist itemlocale
- language of translationtitle
- name of your productunits
- in what units your product can be calculatedpricelist_pricelist_item. Pivot table for pricelists and pricelist_items in many to many relationship. Also contains field 'item_order', which defines order of an element within the table.
pricelistables. Polymorphic many to many relationship table. Responsible for attaching a pricelist to your pricelistable models.
To let your models be able to attach pricelists, add the HasPricelist trait to your model class
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Nestermaks\LaravelPricelist\HasPricelist;
class Offer extends Model
{
use HasPricelist;
...
}
You can associate a pricelist with a model like this:
$offer = Offer::first();
$pricelist = Pricelist::first();
$offer->addPricelist($pricelist);
Or you can detach pricelist from a model:
$offer->removePricelist($pricelist);
To watch all pricelists of the model:
$offer->pricelists();
To show all active pricelists:
Pricelist::getActiveItems();
The same for pricelist items
PricelistItem::getActiveItems();
To show pricelist items related to the pricelist:
Pricelist::relatedItems();
To show pricelists where the pricelist item is present:
PricelistItem::relatedItems();
Assuming we have such variables:
$pricelists = Pricelist::all();
$pricelist = Pricelist::first();
$pricelist_items = PricelistItem::all();
$pricelist_item = PricelistItem::first();
To attach items to the pricelist:
$pricelist->attachItems($pricelist_items);
Or to attach one pricelist item to many pricelists:
$pricelist_item->attachItems($pricelists);
To detach items from the pricelist:
$pricelist->detachItems($pricelist_items);
To detach item from given pricelists:
$pricelist_item->detachItems($pricelists);
Notice that the argument in all these cases is a Collection.
When you attach pricelist items to the pricelist, each of them gets its order number. To see order number of a pricelist item in the pricelist use:
$pricelist->getItemOrder($pricelist_item);
or
$pricelist_item->getItemOrder($pricelist);
To set a new order number of a pricelist item within the pricelist use:
$pricelist->changeItemOrder($pricelist_item, 3);
or
$pricelist_item->changeItemOrder($pricelist, 3);
Second argument is for new order number. After you assign a new order number of the pricelist item, another ones will rearrange appropriately inside the pricelist.
You may use api calls to manage your pricelists. Set desired api prefixes in a config file or leave it default. For pricelists and pricelists items CRUDS are used api resource routes with api resource controllers. You may look through the Laravel DOCS.
Parameters to use in CRUDS:
For example, to update a pricelist with id "12" make a patch request:
https://yoursite.com/nestermaks-api/pricelists/12?active=0&title=web site development&description=here goes description&lang=en&order=10
Parameters to use in CRUDS:
For example, to update a pricelist with id "12" item make a patch request:
https://yoursite.com/nestermaks-api/pricelist-items/12?title=landing page development&units=hour&lang=en&shortcut=land-dev&price=10&max-price=15&price-from=0&active=1
Make a POST request. URL should look like:
https://yoursite.com/nestermaks-api/pricelists/relation
Parameters:
One of the parameters of pricelist_id and pricelist_items_id should be an integer while another one should be an array.
Example:
https://yoursite.com/nestermaks-api/pricelists/relation?action=AttachItems&pricelist_id=2&pricelist_item_id=[3,4,5,6]
Make a POST request. URL should look like:
https://yoursite.com/nestermaks-api/pricelists/model
Parameters:
Example:
https://yoursite.com/nestermaks-api/pricelists/model?action=addPricelist&model_name=App\Models\User&model_id=2&pricelist_id=4
Make a GET request. URL should look like:
https://yoursite.com/nestermaks-api/pricelists/get/{model_name}/{model_id}
Parameters:
Example:
https://yoursite.com/nestermaks-api/pricelists/get/App\Models\User/3
Make a POST request. URL should look like:
https://yoursite.com/nestermaks-api/pricelists/change-order
Parameters:
Example:
https://yoursite.com/nestermaks-api/pricelists/change-order?pricelist_id=3&pricelist_item_id=7&item_order=5
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.