where(['site_id'=> Session::get("user_site_id")]); } } public function Sites() { return $this->belongsTo('Sites', 'site_id')->field('id,name'); } public function Archives() { //return $this->belongsTo('Archives', 'id', 'channel_id')->field('channel_id,count(1) as items'); return $this->hasOne('Archives','channel_id','id')->field('channel_id,count(1) as items'); } /** * 读取分类类型 * @return array */ public static function getTypeList() { //$typelist = config('site.categorytype'); return ['channel'=>__('Channel'), 'list'=>__('List'), 'redirect'=>__('Redirect')]; } public static function getFlagList() { return ['hot' => __('Hot'), 'recommend' => __('Recommend')]; } public function getFlagTextAttr($value, $data) { if(!isset($data['flag'])) return ''; $value = $value ? $value : $data['flag']; $valueArr = explode(',', $value); $list = $this->getFlagList(); return implode(',', array_intersect_key($list, array_flip($valueArr))); } /** * 读取分类列表 * @param string $type 指定类型 * @param string $status 指定状态 * @return array */ public static function getChannelArray($type = NULL, $status = NULL) { $list = collection(self::where(function($query) use($type, $status) { if (!is_null($type)) { $query->where('type', '=', $type); } if (!is_null($status)) { $query->where('status', '=', $status); } })->order('weigh', 'desc')->select())->toArray(); return $list; } /* protected static function init() { self::afterInsert(function ($row) { $pk = $row->getPk(); $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]); }); } */ }