"int", // 主键id "tid" => "int", // 标签id "aid" => "int", // 文章id "cid" => "int", // 栏目id ]; protected $autoWriteTimestamp = false; public function article() { return $this->belongsTo('Article', 'aid')->bind(['title', 'titlepic', 'summary', 'hits', 'create_time', 'username']); } public function category() { return $this->belongsTo('Category', 'cid')->bind(['category_url' => 'url', 'category_name' => 'name']); } public static function queryList($tagid) { return self::with(['article', 'category'])->where('tid', $tagid)->order('aid DESC')->paginate(); } public static function saveArticleTag(array $tags, int $aid, int $cid) { foreach ($tags as $tagname) { $tag = Tag::where('tagname', $tagname)->findOrEmpty(); if ($tag->isEmpty()) { $tag->tagname = $tagname; } $tag->nums += 1; $tag->save(); self::create([ 'tid' => $tag->id, 'aid' => $aid, 'cid' => $cid ]); } } public static function delArticleTag(array $tags, int $aid) { foreach ($tags as $tagname) { $tag = Tag::where('tagname', $tagname)->findOrEmpty(); if ($tag != null) { $tag->nums = $tag->nums - 1; $tag->nums = $tag->nums > 0 ? $tag->nums : 0; $tag->save(); self::where(['aid'=>$aid, 'tid'=>$tag->id])->delete(); } } } }