Completed
Push — master ( 392bd3...60208d )
by ARCANEDEV
04:52
created

Category   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 88
ccs 0
cts 17
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A posts() 0 4 1
A setNameAttribute() 0 5 1
A getSelectOptions() 0 9 2
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanesoft\Blog\Bases\Model;
4
use Illuminate\Database\Eloquent\SoftDeletes;
5
use Illuminate\Support\Facades\Cache;
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
class Category extends Model
21
{
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Traits
24
     | ------------------------------------------------------------------------------------------------
25
     */
26
    use SoftDeletes;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Properties
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * The database table used by the model
34
     *
35
     * @var string
36
     */
37
    protected $table    = 'categories';
38
39
    /**
40
     * The attributes that are mass assignable
41
     *
42
     * @var array
43
     */
44
    protected $fillable = ['name'];
45
46
    /**
47
     * Set or unset the timestamps for the model
48
     *
49
     * @var bool
50
     */
51
    public $timestamps  = true;
52
53
    /**
54
     * The attributes that should be mutated to dates.
55
     *
56
     * @var array
57
     */
58
    protected $dates    = ['deleted_at'];
59
60
    /* ------------------------------------------------------------------------------------------------
61
     |  Relationships
62
     | ------------------------------------------------------------------------------------------------
63
     */
64
    /**
65
     * Relationship with posts.
66
     *
67
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
68
     */
69
    public function posts()
70
    {
71
        return $this->hasMany(Post::class);
72
    }
73
74
    /* ------------------------------------------------------------------------------------------------
75
     |  Getters & Setters
76
     | ------------------------------------------------------------------------------------------------
77
     */
78
    /**
79
     * Set the name attribute.
80
     *
81
     * @param  string  $name
82
     */
83
    public function setNameAttribute($name)
84
    {
85
        $this->attributes['name'] = $name;
86
        $this->attributes['slug'] = str_slug($name);
87
    }
88
89
    /* ------------------------------------------------------------------------------------------------
90
     |  Main Functions
91
     | ------------------------------------------------------------------------------------------------
92
     */
93
    /**
94
     * Get the categories options for select input.
95
     *
96
     * @return array
97
     */
98
    public static function getSelectOptions($placehoder = true)
99
    {
100
        $options    = $placehoder ? ['-- Select a category --'] : [];
101
        $categories = Cache::remember('blog_categories_select_options', 5, function () {
102
            return self::lists('name', 'id');
103
        });
104
105
        return $options + $categories->toArray();
106
    }
107
}
108