-
Notifications
You must be signed in to change notification settings - Fork 3
/
Article.php
71 lines (56 loc) · 1.47 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
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use App\Libraries\Auth;
class Article extends Model
{
protected $table = 'articles';
protected $primaryKey = 'Id';
private $auth;
public function __construct()
{
parent::__construct();
$this->auth = Auth::create();
}
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
public function scopeVisible($query)
{
return $query->where('is_hidden', 0);
}
public function scopeAuth($query)
{
if ($this->auth->isAuthed()) {
return $query;
} else {
return $query->visible()->published();
}
}
public function tags()
{
return $this->belongsToMany('App\Tag', 'article_tags', 'article_id', 'tag_id');
}
public function comments()
{
return $this->hasMany('App\Comment', 'article_id', 'Id');
}
public function cate()
{
return $this->belongsTo('App\Cate', 'cate_id', 'Id');
}
public function prev()
{
$pubtime = $this->published_at;
return Self::where('published_at', '>', $pubtime)->auth()
->orderBy('published_at', 'asc')->first();
}
public function next()
{
$pubtime = $this->published_at;
return Self::where('published_at', '<', $pubtime)->auth()
->orderBy('published_at', 'desc')->first();
}
}