refactor: 产品详细接口关联产品字段添加spu返回

This commit is contained in:
2025-02-12 15:36:11 +08:00
parent 6aaae79952
commit 487ecdd0e8
3 changed files with 14 additions and 3 deletions

View File

@@ -91,8 +91,13 @@ class Product
'related_product_id',
'sort'
])
->productId($product->id
)->select();
->with(['product' => function($query) {
$query->field(['id', 'spu']);
}])
->productId($product->id)
->select()
->bindAttr('product', ['spu'])
->hidden(['product']);
return success('获取成功', $product);
}

View File

@@ -22,7 +22,7 @@ class ProductModel extends ProductBaseModel
// 分类关联查询
public function category()
{
return $this->belongsTo('ProductCategoryModel', 'category_id', 'id');
return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id');
}
// 搜索器名称

View File

@@ -11,6 +11,12 @@ use app\common\model\ProductRelatedBaseModel;
*/
class ProductRelatedModel extends ProductRelatedBaseModel
{
// 关联产品模型
public function product()
{
return $this->belongsTo(ProductModel::class, 'related_product_id', 'id');
}
// 根据产品ID查询
public function scopeProductId($query, $id)
{