商品列表、分类管理、客户等级
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
@@ -28,7 +27,7 @@ class CustomerLevelModel extends Model
|
||||
'name',
|
||||
'sort',
|
||||
'status',
|
||||
'icon'
|
||||
'icon_id'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -45,15 +44,16 @@ class CustomerLevelModel extends Model
|
||||
*/
|
||||
public function icon(): HasOne
|
||||
{
|
||||
return $this->hasOne(SysFileModel::class, 'id', 'icon');
|
||||
return $this->hasOne(SysFileModel::class, 'id', 'icon_id');
|
||||
}
|
||||
|
||||
// 图标链接
|
||||
public function icon_url(): Attribute
|
||||
public function getIconUrlAttribute()
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => $this->icon->preview_url,
|
||||
);
|
||||
if($this->icon) {
|
||||
return $this->icon->preview_url;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
@@ -27,34 +26,37 @@ class ProductCategoryModel extends Model
|
||||
'name',
|
||||
'sort',
|
||||
'status',
|
||||
'icon'
|
||||
'icon_id'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'status' => 'integer',
|
||||
'icon' => 'integer',
|
||||
'icon_id' => 'integer',
|
||||
'created_at' => 'datetime:Y-m-d H:i:s',
|
||||
'updated_at' => 'datetime:Y-m-d H:i:s',
|
||||
];
|
||||
|
||||
protected $appends = ['icon_url'];
|
||||
|
||||
protected $with = ['icon'];
|
||||
|
||||
/**
|
||||
* 关联图标
|
||||
*/
|
||||
public function icon(): HasOne
|
||||
{
|
||||
return $this->hasOne(SysFileModel::class, 'id', 'icon');
|
||||
return $this->hasOne(SysFileModel::class, 'id', 'icon_id');
|
||||
}
|
||||
|
||||
// 图标链接
|
||||
public function icon_url(): Attribute
|
||||
public function getIconUrlAttribute()
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => $this->icon->preview_url,
|
||||
);
|
||||
if($this->icon) {
|
||||
return $this->icon->preview_url;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,14 +89,14 @@ class ProductCategoryModel extends Model
|
||||
* @param bool $onlyEnabled 是否仅返回启用分类
|
||||
* @return array<int, array{id: int, name: string, parent_id: int, sort: int, status: int, children?: array}>
|
||||
*/
|
||||
public static function getTreeData(bool $onlyEnabled = false): array
|
||||
public static function getTreeData($columns = ['*'], bool $onlyEnabled = false): array
|
||||
{
|
||||
$query = static::query()->orderBy('sort')->orderBy('id');
|
||||
if ($onlyEnabled) {
|
||||
$query->where('status', self::STATUS_NORMAL);
|
||||
}
|
||||
|
||||
return static::buildTree($query->get()->toArray());
|
||||
return static::buildTree($query->get($columns)->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,7 +31,7 @@ class ProductModel extends Model
|
||||
'name',
|
||||
'spec',
|
||||
'unit',
|
||||
'images',
|
||||
'image_ids',
|
||||
'content',
|
||||
'sort',
|
||||
'shelf_life',
|
||||
@@ -47,22 +47,32 @@ class ProductModel extends Model
|
||||
'stock' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'status' => 'integer',
|
||||
'created_at' => 'datetime:Y-m-d H:i:s',
|
||||
'updated_at' => 'datetime:Y-m-d H:i:s',
|
||||
];
|
||||
|
||||
public function images(): Attribute
|
||||
protected $appends = ['images_arr'];
|
||||
|
||||
/**
|
||||
* 封面图片
|
||||
*/
|
||||
public function imageIds(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function($value){
|
||||
if (empty($value)) {
|
||||
return collect();
|
||||
}
|
||||
$ids = explode(',', $value);
|
||||
return SysFileModel::whereIn('id', $ids)->get();
|
||||
},
|
||||
get: fn ($value) => explode(',', $value),
|
||||
set: fn ($value) => is_array($value) ? implode(',', $value) : $value,
|
||||
);
|
||||
}
|
||||
|
||||
// 获取封面图片关联数据
|
||||
public function getImagesArrAttribute(): array
|
||||
{
|
||||
if( $this->image_ids ) {
|
||||
return SysFileModel::whereIn('id', $this->image_ids )->get()->toArray();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 所属分类
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user