Completed
Push — master ( 19e1c8...a74d0d )
by Christopher
01:06
created

Post::category()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        return $this->morphMany(Comment::class, 'commentable');
44
    }
45
46
    public function approvedComments()
47
    {
48
        return $this->comments()->where('is_approved', true);
49
    }
50
51
    public function disapprovedComments()
52
    {
53
        return $this->comments()->where('is_approved', false);
54
    }
55
56
    public function tags()
57
    {
58
        return $this->morphToMany(Tag::class, 'taggable');
59
    }
60
61
    public static function boot()
62
    {
63
        parent::boot();
64
65
        static::deleted(function (Model $post) {
66
            $post->tags()->detach();
67
        });
68
    }
69
70
    public function path()
71
    {
72
        return "/posts/{$this->slug}";
73
    }
74
}
75