Ai
4 Star 1 Fork 0

Gitee 极速下载/tipask

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/sdfsky/tipask
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Tag.php 2.74 KB
一键复制 编辑 原始数据 按行查看 历史
sdfsky 提交于 2019-10-17 13:53 +08:00 . remove course and videmo
<?php
namespace App\Models;
use App\Models\Relations\BelongsToCategoryTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
class Tag extends Model
{
use BelongsToCategoryTrait;
protected $table = 'tags';
protected $fillable = ['name', 'logo', 'description','category_id','followers'];
public static function boot()
{
parent::boot();
static::saved(function($tag){
if(Setting()->get('xunsearch_open',0) == 1) {
App::offsetGet('search')->update($tag);
}
});
/*监听删除事件*/
static::deleted(function($tag){
/*删除关注*/
Attention::where('source_type','=',get_class($tag))->where('source_id','=',$tag->id)->delete();
$tag->userTags()->delete();
/*删除用户标签*/
UserTag::where('tag_id','=',$tag->id)->delete();
if(Setting()->get('xunsearch_open',0) == 1){
App::offsetGet('search')->delete($tag);
}
});
}
/**通过字符串添加标签
* @param $tagString
* @param $question_id
*/
public static function multiSave($tagString,$taggable)
{
$tags = array_unique(explode(",",$tagString));
/*删除所有标签关联*/
if($tags){
$taggable->tags()->detach();
}
foreach($tags as $tag_name){
if(!trim($tag_name)){
continue;
}
$tag = self::firstOrCreate(['name'=>$tag_name]);
if(!$taggable->tags->contains($tag->id))
{
$taggable->tags()->attach($tag->id);
}
}
return $tags;
}
/*搜索*/
public static function search($word,$size=16)
{
$list = self::where('name','like',"$word%")->paginate($size);
return $list;
}
public function questions()
{
return $this->morphedByMany('App\Models\Question', 'taggable');
}
public function articles()
{
return $this->morphedByMany('App\Models\Article', 'taggable');
}
public function followers()
{
return $this->morphToMany('App\Models\UserData', 'source','attentions','source_id','user_id');
}
public function userTags(){
return $this->hasMany('App\Models\UserTag','tag_id');
}
/*相关标签检索*/
public function relations($pageSize=25)
{
return self::where(function($query){
$query->where('parent_id','=',$this->parent_id)
->where('id','<>',$this->id);
})->orWhere('parent_id','=',$this->parent_id)
->orderBy('followers','desc')->take($pageSize)->get();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/mirrors/tipask.git
git@gitee.com:mirrors/tipask.git
mirrors
tipask
tipask
master

搜索帮助