Completed
Push — master ( 1a56f9...9378a2 )
by Mohamed
17:07
created

Setting::scopeGetValueFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\MediaLibrary\HasMedia;
8
use Spatie\MediaLibrary\InteractsWithMedia;
9
10
class Setting extends Model implements HasMedia
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addMedia, addMediaConversion, clearMediaCollection, clearMediaCollectionExcept, copyMedia, getMedia, hasMedia, loadMedia, media, registerAllMediaConversions, registerMediaCollections, registerMediaConversions, shouldDeletePreservingMedia
Loading history...
11
{
12
    use InteractsWithMedia;
13
14
    /**
15
     * The attributes that are mass assignable.
16
     *
17
     * @var array
18
     */
19
    protected $fillable = [
20
        'key', 'name', 'value', 'cast'
21
    ];
22
23
    /**
24
     * The attributes that should be cast.
25
     *
26
     * @var array
27
     */
28
    protected $casts = [
29
        'cast' => 'collection'
30
    ];
31
32
    /**
33
     * The accessors to append to the model's array form.
34
     *
35
     * @var array
36
     */
37
    protected $appends = [
38
        'type', 'extra'
39
    ];
40
41
    /**
42
     * Get field's type.
43
     *
44
     * @return string
45
     */
46
    public function getTypeAttribute()
47
    {
48
        return $this->cast->get('type', 'text');
49
    }
50
51
    /**
52
     * Get Extra field's attributes.
53
     *
54
     * @return array
55
     */
56
    public function getExtraAttribute()
57
    {
58
        return json_decode($this->cast->get('extra', '{}'), true);
59
    }
60
61
    /**
62
     * Return the value for the given key.
63
     *
64
     * @param Builder $query
65
     * @param string $key
66
     * @param null $default
67
     * @return string|null
68
     */
69
    public function scopeGetValueFor(Builder $query, $key, $default = null)
70
    {
71
        return optional($query->where('key', $key)->first())->value ?? $default;
72
    }
73
}
74