webpress / product-manager
Webpress 产品管理器
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
Laravel 框架中管理产品的产品管理包
安装
Composer
要将此包包含到您的项目中,请运行以下命令。
composer require vicoders/product_management
服务提供者
在您的 config/app.php
文件中,将以下服务提供者添加到 providers
数组末尾
'providers' => [ ... VCComponent\Laravel\Product\Providers\ProductServiceProvider::class, VCComponent\Laravel\Product\Providers\ProductRouteProvider::class, ],
配置和迁移
运行以下命令以发布配置和迁移文件。
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"
创建表
php artisan migrate
环境
在 .env
文件中,我们需要一些配置。
API_PREFIX=api
API_VERSION=v1
API_NAME="Your API Name"
API_DEBUG=false
配置
URL 命名空间
为了避免与您的应用程序的 api 端点重复,该包为其路由有一个默认命名空间,为 product-management
。例如
{{url}}/api/product-management/admin/product
您可以通过修改 .env
文件中的 PRODUCT_COMPONENT_NAMESPACE
变量来将包的 URL 命名空间修改为您想要的任何内容。
PRODUCT_COMPONENT_NAMESPACE="your-namespace"
模型和转换器
您可以通过修改配置文件 config\product.php
来使用自己的模型和转换器类
'models' => [ 'product' => App\Entities\Product::class, ], 'transformers' => [ 'product' => App\Transformers\ProductTransformer::class, ],
您的 Product
模型类必须实现 VCComponent\Laravel\Product\Contracts\ProductSchema
和 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', ]; }
认证中间件
在配置文件 config\product.php
中配置认证中间件
'auth_middleware' => [ 'admin' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], 'frontend' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], ],
如果您有额外的字段,只需将 schema
添加到 Product
模型类中。
public function schema() { return [ 'regular_price' => [ 'type' => 'text', 'rule' => ['nullable'], ] ]; }
提供查询函数
存储库
查询函数列表
按字段查找
public function findProductByField($field, $value)
按条件数组查找产品
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
按 id 获取产品
public function getProductByID($product_id);
按大小获取产品图片列表
public function getProductMedias($product_id, $image_dimension= '');
获取产品链接
public function getProductUrl($product_id);
获取同一分类的相关产品
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
按分类获取产品
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
按关键字搜索产品
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
使用
在控制器中使用 PostRepository
并添加 __construct
函数
use VCComponent\Laravel\Product\Repositories\ProductRepository;
public function __construct(ProductRepository $productRepo) { $this->productRepo = $productRepo; }
例如
$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
实体
实体查询函数列表
将查询范围限定为仅包括特定类型的产品。
public function scopeOfType($query, $type)
按类型获取产品集合。
public static function getByType($type = 'products')
按类型和分页获取产品。
public static function getByTypeWithPagination($type = 'products', $per_page = 15)
按类型和ID获取产品。
public static function findByType($id, $type = 'products')
获取产品元数据。
public function getMetaField($key)
将查询范围限定为仅包括热门产品。
public function scopeIsHot($query)
将查询范围限定为仅包括有库存的产品。
public function scopeInStock($query)
将查询范围限定为仅包括已发布的产品。
public function scopeIsPublished($query)
将查询范围限定为按订单列排序产品。
public function scopeSortByOrder($query, $order = 'desc')
将查询范围限定为按发布日期列排序产品。
public function scopeSortByPublishedDate($query, $order = 'desc')
将查询范围限定为按销量排序产品。
public function scopeSortBySoldQuanlity($query, $order = 'desc')
将查询范围限定为搜索给定关键字的产品。此函数还可以与分类或标签一起使用。
public function scopeOfSearching($query, $search, $with_category = false, $with_tag = false)
将查询范围限定为包括相关产品。此函数还可以与分类或标签一起使用。
public function scopeOfRelatingTo($query, $product, $with_category = false, $with_tag = false)
使用实体查询函数
使用特性。
namespace App\Model; use VCComponent\Laravel\Product\Traits\ProductQueryTrait; class Product { use ProductQueryTrait; \\ }
扩展 VCComponent\Laravel\Product\Entities\Product
实体。
namespace App\Model; use VCComponent\Laravel\Product\Entities\Product as BaseProduct; class Product extends BaseProduct { \\ }
实体查询函数示例
$product = Product::isPublished()->inStock()->with('categories')->sortBySoldQuanlity()->first(); $products = Product::ofType('product')->isPublished()->inStock()->ofRelatingTo($product, true)->get();
视图
您的 ProductListController
控制器类必须扩展 VCComponent\Laravel\Product\Http\Controllers\Web\ProductListController as BaseProductListController
并实现 VCComponent\Laravel\Product\Contracts\ViewProductListControllerInterface;
class ProductListController extends BaseProductListController implements ViewProductListControllerInterface { }
您的 ProductDetailController
控制器类必须扩展 VCComponent\Laravel\Product\Http\Controllers\Web\ProductDetailController as BaseProductDetailController
并实现 VCComponent\Laravel\Product\Contracts\ViewProductDetailControllerInterface;
class ProductDetailController extends BaseProductDetailController implements ViewProductDetailControllerInterface { }
如果您想更改默认视图,您必须将视图添加到 Product
控制器类中。
protected function view() { return 'view-custom'; }
路由
API端点应具有以下格式
动词 | 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} |