79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\model\v1;
|
|
|
|
use app\common\model\SysBannerItemBaseModel;
|
|
use think\model\concern\SoftDelete;
|
|
|
|
/**
|
|
* 横幅数据项模型
|
|
* @mixin \think\Model
|
|
*/
|
|
class SysBannerItemModel extends SysBannerItemBaseModel
|
|
{
|
|
// 启用软删除
|
|
use SoftDelete;
|
|
// 软删除字段
|
|
protected $deleteTime = 'deleted_at';
|
|
|
|
// 绑定产品分类关联模型中字段到父模型
|
|
protected $append = ['rel_prod_cate_id', 'rel_prod_cate_name'];
|
|
// 要隐藏的字段或关联模型数据字段
|
|
protected $hidden = ['prodMapping'];
|
|
|
|
// 关联分类
|
|
public function banner()
|
|
{
|
|
return $this->belongsTo(SysBannerModel::class, 'banner_id', 'id');
|
|
}
|
|
|
|
// 关联相关产品分类中间表模型
|
|
public function prodMapping()
|
|
{
|
|
return $this->hasOne(SysBannerProdCateMappingModel::class, 'banner_item_id', 'id');
|
|
}
|
|
|
|
// 从产品分类关联模型中获取id字段
|
|
public function getRelProdCateIdAttr()
|
|
{
|
|
return $this->prodMapping?->category?->id;
|
|
}
|
|
// 从产品分类关联模型中获取name字段
|
|
public function getRelProdCateNameAttr()
|
|
{
|
|
return $this->prodMapping?->category?->name;
|
|
}
|
|
|
|
// 按横幅标题搜索
|
|
public function searchTitleAttr($query, $value, $data)
|
|
{
|
|
if (empty($value)) {
|
|
return;
|
|
}
|
|
$query->where('title', 'like', "%{$value}%");
|
|
}
|
|
|
|
// 按添加时间搜索
|
|
public function searchCreatedAtAttr($query, $value, $data)
|
|
{
|
|
if (empty($value)) {
|
|
return;
|
|
}
|
|
if (is_array($value)) {
|
|
if (count($value) > 1) {
|
|
$query->whereBetweenTime ('created_at', $value[0], $value[1]);
|
|
} else {
|
|
$query->whereTime('created_at', '>=', $value[0]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 根据横幅id查询
|
|
public function scopeBannerId($query, $value)
|
|
{
|
|
if (is_null($value)) return;
|
|
$query->where('banner_id', '=', $value);
|
|
}
|
|
}
|