Completed
Push — master ( b2af6e...0cd744 )
by ARCANEDEV
04:46
created

Category::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Illuminate\Database\Eloquent\SoftDeletes;
4
use Illuminate\Support\Facades\Cache;
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class     Category
9
 *
10
 * @package  Arcanesoft\Blog\Models
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  int             id
14
 * @property  string          name
15
 * @property  string          slug
16
 * @property  \Carbon\Carbon  created_at
17
 * @property  \Carbon\Carbon  updated_at
18
 * @property  \Carbon\Carbon  deleted_at
19
 *
20
 * @property  \Illuminate\Database\Eloquent\Collection  posts
21
 */
22
class Category extends AbstractModel
23
{
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Traits
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    use SoftDeletes;
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Properties
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * The database table used by the model
36
     *
37
     * @var string
38
     */
39
    protected $table    = 'categories';
40
41
    /**
42
     * The attributes that are mass assignable
43
     *
44
     * @var array
45
     */
46
    protected $fillable = ['name'];
47
48
    /**
49
     * The attributes that should be mutated to dates.
50
     *
51
     * @var array
52
     */
53
    protected $dates    = ['deleted_at'];
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Relationships
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Relationship with posts.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
63
     */
64
    public function posts()
65
    {
66
        return $this->hasMany(Post::class);
67
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Getters & Setters
71
     | ------------------------------------------------------------------------------------------------
72
     */
73
    /**
74
     * Set the name attribute.
75
     *
76
     * @param  string  $name
77
     */
78
    public function setNameAttribute($name)
79
    {
80
        $this->attributes['name'] = $name;
81
        $this->attributes['slug'] = Str::slug($name);
82
    }
83
84
    /* ------------------------------------------------------------------------------------------------
85
     |  Main Functions
86
     | ------------------------------------------------------------------------------------------------
87
     */
88
    /**
89
     * Get the categories options for select input.
90
     *
91
     * @param  bool  $placeholder
92
     *
93
     * @return \Illuminate\Database\Eloquent\Collection
94
     */
95
    public static function getSelectOptions($placeholder = true)
96
    {
97
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
98
        $categories = Cache::remember('blog_categories_select_options', 5, function () {
99
            return self::all()->pluck('name', 'id');
100
        });
101
102
        return $placeholder
103
            ? $categories->prepend('-- Select a category --', 0)
104
            : $categories;
105
    }
106
107
    /* ------------------------------------------------------------------------------------------------
108
     |  Check Functions
109
     | ------------------------------------------------------------------------------------------------
110
     */
111
    /**
112
     * Check if category has posts.
113
     *
114
     * @return bool
115
     */
116
    public function hasPosts()
117
    {
118
        return ! $this->posts->isEmpty();
119
    }
120
121
    /* ------------------------------------------------------------------------------------------------
122
     |  Other Functions
123
     | ------------------------------------------------------------------------------------------------
124
     */
125
    /**
126
     * Clear the cached categories.
127
     */
128
    public static function clearCache()
129
    {
130
        cache()->forget('blog_categories_select_options');
131
    }
132
}
133