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

Tag::setNameAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
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 Tag 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 = 'tags';
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
     * Posts relationship.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
63
     */
64
    public function posts()
65
    {
66
        return $this->belongsToMany(Post::class, "{$this->prefix}post_tag");
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
     * @return \Illuminate\Database\Eloquent\Collection
92
     */
93
    public static function getSelectOptions()
94
    {
95
        return Cache::remember('blog_tags_select_options', 5, function () {
96
            return self::all()->pluck('name', 'id');
97
        });
98
    }
99
100
    /* ------------------------------------------------------------------------------------------------
101
     |  Check Functions
102
     | ------------------------------------------------------------------------------------------------
103
     */
104
    /**
105
     * Check if tag has posts.
106
     *
107
     * @return bool
108
     */
109
    public function hasPosts()
110
    {
111
        return ! $this->posts->isEmpty();
112
    }
113
114
    /* ------------------------------------------------------------------------------------------------
115
     |  Other Functions
116
     | ------------------------------------------------------------------------------------------------
117
     */
118
    /**
119
     * Clear the cached tags.
120
     */
121
    public static function clearCache()
122
    {
123
        cache()->forget('blog_tags_select_options');
124
    }
125
}
126