webpress / product-manager
Webpress product manager
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 2 721
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 10
Open Issues: 1
Requires
- cviebrock/eloquent-sluggable: 7.0.1
- webpress/category-manager: ^3.1
- webpress/core: ^3.1
- webpress/export-excel: ^3.1
- webpress/tag-manager: ^3.1
- webpress/user-manager: ^3.1
- webpress/view-model: ^3.1
Requires (Dev)
- orchestra/testbench: 5.0.0
- dev-master
- 3.1.87
- 3.1.86
- 3.1.85
- 3.1.83
- 3.1.82
- 3.1.81
- 3.1.80
- 3.1.79
- 3.1.78
- 3.1.77
- 3.1.76
- 3.1.74
- 3.1.73
- 3.1.72
- 3.1.71
- 3.1.70
- 3.1.69
- 3.1.68
- 3.1.67
- 3.1.66
- 3.1.65
- 3.1.64
- 3.1.63
- 3.1.61
- 3.1.60
- 3.1.59
- 3.1.58
- 3.1.57
- 3.1.56
- 3.1.54
- 3.1.53
- 3.1.52
- 3.1.51
- 3.1.50
- 3.1.49
- 3.1.48
- 3.1.47
- 3.1.46
- 3.1.45
- 3.1.44
- 3.1.43
- 3.1.42
- 3.1.41
- 3.1.40
- 3.1.39
- 3.1.38
- 3.1.37
- 3.1.36
- 3.1.35
- 3.1.34
- 3.1.33
- 3.1.32
- 3.1.31
- 3.1.30
- 3.1.29
- 3.1.28
- 3.1.27
- 3.1.26
- 3.1.25
- 3.1.24
- 3.1.23
- 3.1.22
- 3.1.20
- 3.1.19
- 3.1.18
- 3.1.17
- 3.1.15
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.34
- 3.0.33
- 3.0.32
- 3.0.31
- 3.0.30
- 3.0.29
- 3.0.28
- 3.0.27
- 3.0.26
- 3.0.25
- 3.0.24
- 3.0.23
- 3.0.22
- 3.0.21
- 3.0.20
- 3.0.19
- 3.0.18
- 3.0.17
- 3.0.16
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.6
- 3.0.5
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.1
- 0.19.0
- 0.18.0
- 0.17.0
- 0.16.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.19
- 0.0.18
- 0.0.17
- 0.0.16
- 0.0.15
- 0.0.14
- 0.0.13
- 0.0.12
- 0.0.11
- 0.0.10
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-feat/list-product-filter-pipes
- dev-feat/add-product-variant-thumbnail
- dev-dev/v9.0
- dev-develop
- dev-feature/dynamic-product-schema
- dev-feature/integration-test-api
This package is auto-updated.
Last update: 2022-06-17 10:31:30 UTC
README
Product management package for managing product in laravel framework
Installation
Composer
To include the package in your project, Please run following command.
composer require vicoders/product_management
Service Provider
In your config/app.php
add the following Service Providers to the end of the providers
array:
'providers' => [ ... VCComponent\Laravel\Product\Providers\ProductServiceProvider::class, VCComponent\Laravel\Product\Providers\ProductRouteProvider::class, ],
Config and Migration
Run the following commands to publish configuration and migration files.
php artisan vendor:publish --provider="VCComponent\Laravel\Product\Providers\ProductServiceProvider"
php artisan vendor:publish --provider="Dingo\Api\Provider\LaravelServiceProvider"
php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider"
Create tables:
php artisan migrate
Environment
In .env
file, we need some configuration.
API_PREFIX=api
API_VERSION=v1
API_NAME="Your API Name"
API_DEBUG=false
Configuration
URL namespace
To avoid duplication with your application's api endpoints, the package has a default namespace for its routes which is product-management
. For example:
{{url}}/api/product-management/admin/product
You can modify the package url namespace to whatever you want by modifying the PRODUCT_COMPONENT_NAMESPACE
variable in .env
file.
PRODUCT_COMPONENT_NAMESPACE="your-namespace"
Model and Transformer
You can use your own model and transformer class by modifying the configuration file config\product.php
'models' => [ 'product' => App\Entities\Product::class, ], 'transformers' => [ 'product' => App\Transformers\ProductTransformer::class, ],
Your Product
model class must implements VCComponent\Laravel\Product\Contracts\ProductSchema
and VCComponent\Laravel\Product\Contracts\ProductManagement
<?php namespace App\Entities; use Illuminate\Database\Eloquent\Model; use Prettus\Repository\Contracts\Transformable; use Prettus\Repository\Traits\TransformableTrait; use VCComponent\Laravel\Product\Contracts\ProductManagement; use VCComponent\Laravel\Product\Contracts\ProductSchema; use VCComponent\Laravel\Product\Traits\ProductManagementTrait; use VCComponent\Laravel\Product\Traits\ProductSchemaTrait; class Product extends Model implements Transformable, ProductSchema, ProductManagement { use TransformableTrait, ProductSchemaTrait, ProductManagementTrait; const STATUS_PENDING = 1; const STATUS_ACTIVE = 2; protected $fillable = [ 'name', 'description', 'status', 'slug', 'price', ]; }
Auth middleware
Configure auth middleware in configuration file config\product.php
'auth_middleware' => [ 'admin' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], 'frontend' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], ],
If your has additional fields, just add the schema
to the Product
model class.
public function schema() { return [ 'regular_price' => [ 'type' => 'text', 'rule' => ['nullable'], ] ]; }
Query functions provide
Repository
List of query functions
Find By Field
public function findProductByField($field, $value)
Find products by condition array
public function findByWhere(array $where, $number = 10, $order_by = 'order', $order = 'asc'); public function findByWherePaginate(array $where, $number = 10, $order_by = 'order', $order = 'asc') // Find products by condition array withPaginate
Get product by id
public function getProductByID($product_id);
Get product image list by size
public function getProductMedias($product_id, $image_dimension= '');
Get product link
public function getProductUrl($product_id);
Get related products in the same category
public function getRelatedProducts($product_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc'); public function getRelatedProductsPaginate($product_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc'); // get related products of the same category with pagination
get products by category
public function getProductsWithCategory($category_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc', $columns = ['*']); public function getProductsWithCategoryPaginate($category_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc', $columns = ['*']); // get products by pagination category
Search product by keyword
public function getSearchResult($key_word,array $list_field = ['name'], array $where = [], $category_id = 0,$number = 10,$order_by = 'order', $order = 'asc', $columns = ['*']); public function getSearchResultPaginate($key_word, array $list_field = ['name'], array $where = [], $category_id = 0,$number = 10,$order_by = 'order', $order = 'asc', $columns = ['*']); // Search product by keyword with pagination
Use
At controller use PostRepository
and add function __construct
use VCComponent\Laravel\Product\Repositories\ProductRepository;
public function __construct(ProductRepository $productRepo) { $this->productRepo = $productRepo; }
For example
$product = $this->productRepo->findProductByField('name','product hot'); // get a product named hot product $productWhere = $this->productRepo->findByWhere(['name'=>'product hot','status'=>1]); // get a product named hot product and status = 1 $productWhere = $this->productRepo->findByWherePaginate(['name'=>'product hot','status'=>1]); // get a product named hot product and status = 1 with paginate $productById = $this->productRepo->getProductByID(1); // get product with id = 1 $productMedia = $this->productRepo->getProductMedias(2); // get a list of images of product with id = 2 $product = $this->productRepo->getProductUrl(1); // get the product link with id = 1 $productsRelated = $this->productRepo->getRelatedProducts(1); // get all products in the same category as the product with id = 1 $productsRelatedPaginate = $this->productRepo->getRelatedProductsPaginate(1); // get all products of the same category as the product with id=1 with pagination $productsWithCategory = $this->productRepo->getProductsWithCategory(1); // get all products in category id = 1 $productsWithCategoryPaginate = $this->productRepo->getProductsWithCategory(1); // get all products of category id = 2 with pagination $productsResult = $this->productRepo->getSearchResult('hot',['name','description']); // get all products that contain "hot" in name or description field $productsResult = $this->productRepo->getSearchResult('hot',['name','description'],['status'=>1],3); // get all product that contain "hot" in name or description field and have status = 1 field and belong to category with id = 3 $productsResult = $this->productRepo->getSearchResultPaginate('hot',['name','description'],['status'=>1],3); // get all product that contain "hot" in name or description field and have status = 1 field and belong to category with id = 3 with paginate
Entity
List of entity query function
Scope a query to only include products of a given type.
public function scopeOfType($query, $type)
Get product collection by type.
public static function getByType($type = 'products')
Get product by type with pagination.
public static function getByTypeWithPagination($type = 'products', $per_page = 15)
Get product by type and id.
public static function findByType($id, $type = 'products')
Get product meta data.
public function getMetaField($key)
Scope a query to only include hot products.
public function scopeIsHot($query)
Scope a query to only include in stock products.
public function scopeInStock($query)
Scope a query to only include publisded products.
public function scopeIsPublished($query)
Scope a query to sort products by order column.
public function scopeSortByOrder($query, $order = 'desc')
Scope a query to sort products by published_date column.
public function scopeSortByPublishedDate($query, $order = 'desc')
Scope a query to sort products by sold quanlity.
public function scopeSortBySoldQuanlity($query, $order = 'desc')
Scope a query to search products of given key word. This function is also able to scope with categories, or tags.
public function scopeOfSearching($query, $search, $with_category = false, $with_tag = false)
Scope a query to include related products. This function is also able to scope with categories, or tags.
public function scopeOfRelatingTo($query, $product, $with_category = false, $with_tag = false)
Use entity query function
Use Trait.
namespace App\Model; use VCComponent\Laravel\Product\Traits\ProductQueryTrait; class Product { use ProductQueryTrait; \\ }
Extend VCComponent\Laravel\Product\Entities\Product
Entity.
namespace App\Model; use VCComponent\Laravel\Product\Entities\Product as BaseProduct; class Product extends BaseProduct { \\ }
Entity query function example
$product = Product::isPublished()->inStock()->with('categories')->sortBySoldQuanlity()->first(); $products = Product::ofType('product')->isPublished()->inStock()->ofRelatingTo($product, true)->get();
View
Your ProductListController
controller class must extends VCComponent\Laravel\Product\Http\Controllers\Web\ProductListController as BaseProductListController
implements VCComponent\Laravel\Product\Contracts\ViewProductListControllerInterface;
class ProductListController extends BaseProductListController implements ViewProductListControllerInterface { }
Your ProductDetailController
controller class must extends VCComponent\Laravel\Product\Http\Controllers\Web\ProductDetailController as BaseProductDetailController
implements VCComponent\Laravel\Product\Contracts\ViewProductDetailControllerInterface;
class ProductDetailController extends BaseProductDetailController implements ViewProductDetailControllerInterface { }
If you want change view default, you must add the view your to the Product
controller class.
protected function view() { return 'view-custom'; }
Routes
The api endpoint should have these format:
Verb | URI |
---|---|
GET | /api/{namespace}/admin/products |
GET | /api/{namespace}/admin/products/{id} |
POST | /api/{namespace}/admin/products |
PUT | /api/{namespace}/admin/products/{id} |
DELETE | /api/{namespace}/admin/products/{id} |
PUT | /api/{namespace}/admin/products/status/bulk |
PUT | /api/{namespace}/admin/products/status/{id} |
---- | ---- |
GET | /api/{namespace}/ |
GET | /api/{namespace}/{id} |
POST | /api/{namespace}/ |
PUT | /api/{namespace}/{id} |
DELETE | /api/{namespace}/{id} |
PUT | /api/{namespace}/status/bulk |
PUT | /api/{namespace}/status/{id} |