|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chriscreates\Blog; |
|
4
|
|
|
|
|
5
|
|
|
use Chriscreates\Blog\Builders\PostBuilder; |
|
6
|
|
|
use Chriscreates\Blog\Traits\IsAuthorable; |
|
7
|
|
|
use Chriscreates\Blog\Traits\Post\PostAttributes; |
|
8
|
|
|
use Chriscreates\Blog\Traits\Post\PostsHaveACategory; |
|
9
|
|
|
use Chriscreates\Blog\Traits\Post\PostsHaveComments; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
|
|
12
|
|
|
class Post extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
use PostAttributes, |
|
15
|
|
|
IsAuthorable, |
|
16
|
|
|
PostsHaveComments, |
|
17
|
|
|
PostsHaveACategory; |
|
18
|
|
|
|
|
19
|
|
|
const PUBLISHED = 'published'; |
|
20
|
|
|
const DRAFT = 'draft'; |
|
21
|
|
|
const SCHEDULED = 'scheduled'; |
|
22
|
|
|
|
|
23
|
|
|
protected $table = 'posts'; |
|
24
|
|
|
|
|
25
|
|
|
protected $primaryKey = 'id'; |
|
26
|
|
|
|
|
27
|
|
|
public $guarded = ['id']; |
|
28
|
|
|
|
|
29
|
|
|
public $timestamps = true; |
|
30
|
|
|
|
|
31
|
|
|
protected $appends = ['tagsCount']; |
|
32
|
|
|
|
|
33
|
|
|
protected $dates = ['published_at']; |
|
34
|
|
|
|
|
35
|
|
|
public function category() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->hasOne(Category::class, 'id', 'category_id'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function comments() |
|
41
|
|
|
{ |
|
42
|
|
|
if ( ! $this->allow_comments) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ( ! $this->allow_guest_comments) { |
|
47
|
|
|
return $this->morphMany(Comment::class, 'commentable') |
|
48
|
|
|
->whereNull('user_id'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->morphMany(Comment::class, 'commentable'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function approvedComments() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->comments()->where('is_approved', true); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function disapprovedComments() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->comments()->where('is_approved', false); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function guestComments() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->comments()->whereNull('user_id'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function userComments() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->comments()->whereNotNull('user_id'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function tags() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->morphToMany(Tag::class, 'taggable'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function newEloquentBuilder($query) : PostBuilder |
|
80
|
|
|
{ |
|
81
|
|
|
return new PostBuilder($query); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function path() |
|
85
|
|
|
{ |
|
86
|
|
|
return "/posts/{$this->slug}"; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|