Completed
Push — master ( 2b1674...071d4d )
by ARCANEDEV
05:46
created

Category::createOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

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 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
29
    use SoftDeletes;
30
31
    /* -----------------------------------------------------------------
32
     |  Properties
33
     | -----------------------------------------------------------------
34
     */
35
36
    /**
37
     * The database table used by the model
38
     *
39
     * @var string
40
     */
41
    protected $table    = 'categories';
42
43
    /**
44
     * The attributes that are mass assignable
45
     *
46
     * @var array
47
     */
48
    protected $fillable = ['name'];
49
50
    /**
51
     * The attributes that should be mutated to dates.
52
     *
53
     * @var array
54
     */
55
    protected $dates    = ['deleted_at'];
56
57
    /**
58
     * The attributes that should be cast to native types.
59
     *
60
     * @var array
61
     */
62
    protected $casts = [
63
        'id' => 'integer',
64
    ];
65
66
    /* -----------------------------------------------------------------
67
     |  Relationships
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Relationship with posts.
73
     *
74
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
75
     */
76
    public function posts()
77
    {
78
        return $this->hasMany(Post::class);
79
    }
80
81
    /* -----------------------------------------------------------------
82
     |  Getters & Setters
83
     | -----------------------------------------------------------------
84
     */
85
86
    /**
87
     * Set the name attribute.
88
     *
89
     * @param  string  $name
90
     */
91
    public function setNameAttribute($name)
92
    {
93
        $this->attributes['name'] = $name;
94
        $this->attributes['slug'] = Str::slug($name);
95
    }
96
97
    /* -----------------------------------------------------------------
98
     |  Main Methods
99
     | -----------------------------------------------------------------
100
     */
101
102
    /**
103
     * Create a new category.
104
     *
105
     * @param  array  $inputs
106
     *
107
     * @return self
108
     */
109
    public static function createOne(array $inputs)
110
    {
111
        $category = new self($inputs);
112
113
        $category->save();
114
115
        return $category;
116
    }
117
118
    /**
119
     * Get the categories options for select input.
120
     *
121
     * @param  bool  $placeholder
122
     *
123
     * @return \Illuminate\Database\Eloquent\Collection
124
     */
125
    public static function getSelectOptions($placeholder = true)
126
    {
127
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
128
        $categories = Cache::remember('blog_categories_select_options', 5, function () {
129
            return self::all()->pluck('name', 'id');
130
        });
131
132
        return $placeholder
133
            ? $categories->prepend(trans('blog::categories.select-category'), 0)
134
            : $categories;
135
    }
136
137
    /* -----------------------------------------------------------------
138
     |  Check Methods
139
     | -----------------------------------------------------------------
140
     */
141
142
    /**
143
     * Check if category has posts.
144
     *
145
     * @return bool
146
     */
147
    public function hasPosts()
148
    {
149
        return ! $this->posts->isEmpty();
150
    }
151
152
    /**
153
     * Check if the category is deletable.
154
     *
155
     * @return bool
156
     */
157
    public function isDeletable()
158
    {
159
        return ! $this->hasPosts();
160
    }
161
162
    /* -----------------------------------------------------------------
163
     |  Other Methods
164
     | -----------------------------------------------------------------
165
     */
166
167
    /**
168
     * Clear the cached categories.
169
     */
170
    public static function clearCache()
171
    {
172
        cache()->forget('blog_categories_select_options');
173
    }
174
}
175