/
Article.php
executable file
·128 lines (99 loc) · 3.94 KB
/
Article.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace App\Models;
use App\Models\Relations\BelongsToCategoryTrait;
use App\Models\Relations\BelongsToUserTrait;
use App\Models\Relations\MorphManyCommentsTrait;
use App\Models\Relations\MorphManyTagsTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
class Article extends Model
{
use BelongsToUserTrait,MorphManyTagsTrait,MorphManyCommentsTrait,BelongsToCategoryTrait;
protected $table = 'articles';
protected $fillable = ['title', 'user_id','category_id', 'content','tags','summary','status','logo'];
public static function boot()
{
parent::boot();
/*监听创建*/
static::creating(function($article){
/*开启状态检查*/
if(Setting()->get('verify_article')==1){
$article->status = 0;
}
if( trim($article->summary) === '' ){
$article->summary = str_limit(strip_tags($article->content),180);
}
});
static::saved(function($article){
if(Setting()->get('xunsearch_open',0) == 1){
App::offsetGet('search')->update($article);
}
});
/*监听删除事件*/
static::deleting(function($article){
/*用户文章数 -1 */
$article->user->userData()->where("articles",">",0)->decrement('articles');
Collection::where('source_type','=',get_class($article))->where('source_id','=',$article->id)->delete();
/*删除回答评论*/
Comment::where('source_type','=',get_class($article))->where('source_id','=',$article->id)->delete();
/*删除动态*/
Doing::where('source_type','=',get_class($article))->where('source_id','=',$article->id)->delete();
});
static::deleted(function($article){
if(Setting()->get('xunsearch_open',0) == 1){
App::offsetGet('search')->delete($article);
}
});
}
/*获取相关文章*/
public static function correlations($tagIds,$size=6)
{
$questions = self::where("status",">",0)->whereHas('tags', function($query) use ($tagIds) {
$query->whereIn('tag_id', $tagIds);
})->orderBy('created_at','DESC')->take($size)->get();
return $questions;
}
/*搜索*/
public static function search($word,$size=16)
{
$list = self::where('title','like',"$word%")->paginate($size);
return $list;
}
/*推荐文章*/
public static function recommended($categoryId=0 , $pageSize=20)
{
$query = self::query();
$category = Category::findFromCache($categoryId);
if( $category ){
$query->whereIn('category_id',$category->getSubIds());
}
$list = $query->where('status','>',0)->orderBy('supports','DESC')->orderBy('created_at','DESC')->paginate($pageSize);
return $list;
}
/*热门文章*/
public static function hottest($categoryId=0 , $pageSize=20)
{
$query = self::query();
$category = Category::findFromCache($categoryId);
if( $category ){
$query->whereIn('category_id',$category->getSubIds());
}
if(Setting()->get('hot_content_period',365)){
$query->where('created_at', ">" , Carbon::now()->subDays(Setting()->get('hot_content_period',365)));
}
$list = $query->where('status','>',0)->orderBy('views','DESC')->orderBy('collections','DESC')->orderBy('created_at','DESC')->paginate($pageSize);
return $list;
}
/*最新问题*/
public static function newest($categoryId=0 , $pageSize=20)
{
$query = self::query();
$category = Category::findFromCache($categoryId);
if( $category ){
$query->whereIn('category_id',$category->getSubIds());
}
$list = $query->where('status','>',0)->orderBy('created_at','DESC')->paginate($pageSize);
return $list;
}
}