Test Failed
Push — widgets-pivot ( dc1e33...aef25d )
by Tony
04:08
created

Widget::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * App\Models\UsersWidgets
9
 *
10
 * @property integer $user_widget_id
11
 * @property integer $user_id
12
 * @property integer $widget_id
13
 * @property boolean $col
14
 * @property boolean $row
15
 * @property boolean $size_x
16
 * @property boolean $size_y
17
 * @property string $title
18
 * @property boolean $refresh
19
 * @property string $settings
20
 * @property integer $dashboard_id
21
 * @property-read \App\Models\User $user
22
 * @property-read \App\Models\WidgetDefinition $widget
23
 * @property-read \App\Models\Dashboard $dashboard
24
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereUserWidgetId($value)
25
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereUserId($value)
26
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereWidgetId($value)
27
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereCol($value)
28
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereRow($value)
29
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereSizeX($value)
30
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereSizeY($value)
31
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereTitle($value)
32
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereRefresh($value)
33
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereSettings($value)
34
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget whereDashboardId($value)
35
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Widget getSettings($request)
36
 * @mixin \Eloquent
37
 */
38
class Widget extends Model
39
{
40
    /**
41
     * Indicates if the model should be timestamped.
42
     *
43
     * @var bool
44
     */
45
    public $timestamps = false;
46
    /**
47
     * The table associated with the model.
48
     *
49
     * @var string
50
     */
51
    protected $table = 'users_widgets';
52
    /**
53
     * The primary key column name.
54
     *
55
     * @var string
56
     */
57
    protected $primaryKey = 'user_widget_id';
58
    /**
59
     * The attributes that are mass assignable.
60
     *
61
     * @var array
62
     */
63
    protected $fillable = ['widget_id', 'col', 'row', 'size_x', 'size_y', 'title', 'refresh', 'settings'];
64
65
    // ---- Accessors/Mutators ----
66
67
    /**
68
     * @param string $settings
69
     * @return array
70
     */
71
    public function getSettingsAttribute($settings)
72
    {
73
        return json_decode($settings);
74
    }
75
76
    /**
77
     * @param array $settings
78
     * @return string
79
     */
80
    public function gstSettingsAttribute($settings)
81
    {
82
        return json_encode($settings);
83
    }
84
85
    // ---- Query scopes ----
86
87
    public function scopeGetSettings($query, $request)
88
    {
89
        return $query->where([
90
            ['user_widget_id', '=', $request->id],
91
            ['user_id', '=', $request->user()->user_id]
92
        ])->select('settings');
93
    }
94
95
    // ---- Define Relationships ----
96
97
    /**
98
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
99
     */
100
    public function user()
101
    {
102
        return $this->belongsTo('App\Models\User', 'user_id');
103
    }
104
105
    /**
106
     * Returns the base widget definition
107
     *
108
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
109
     */
110
    public function widget()
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
111
    {
112
        return $this->hasOne('App\Models\Widget', 'widget_id');
113
    }
114
115
    /**
116
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
117
     */
118
    public function dashboard()
119
    {
120
        return $this->belongsTo('App\Models\Dashboard', 'dashboard_id');
121
    }
122
}
123