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