链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

85 lines
1.9 KiB

<?php
namespace App\Models\v3;
use Illuminate\Database\Eloquent\SoftDeletes;
use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use ModelTree;
use SoftDeletes;
protected $table = 'lanzu_category';
protected $dateFormat = 'U';
// 父级ID字段名称,默认值为 parent_id
// protected $parentColumn = 'pid';
// 排序字段名称,默认值为 order
protected $orderColumn = 'sort';
// 标题字段名称,默认值为 title
// protected $titleColumn = 'name';
protected $appends = [
'cover_img_url',
];
public function getCoverImgUrlAttribute($value)
{
$value = $value ? $value : $this->cover_img;
return $this->imageUrl($value);
}
/**
* 获取单个信息
* @param int $id
* @param string $field
* @return string
*/
public static function getCategoryInfo($id,$field = '*')
{
return self::select($field)->find($id);
}
/**
* 获取二级分类数组
* id为键,name为值
* @return array
*/
public static function getCategoryArray($where = [],$options = [])
{
$model = self::select('id','title')
->where('status',1)
->whereNull('deleted_at');
if(count($where) > 0){
$model->where($where);
}
$list = $model->get();
$array = empty($options) ? [] : $options;
if(count($list) > 0){
foreach ($list as $value) {
$array[$value->id] = $value->title;
}
}
return $array;
}
/**
* 处理旧图片
* @param $value
* @return string
*/
public function imageUrl($value)
{
if(strripos($value,"http") === false){
return env('OSS_IMG_HOST').'/'.$value;
}else{
return $value;
}
}
}