|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tracking\Models\Statistics; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Muleta\Recursos\Cacheable\CacheableEloquent; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
10
|
|
|
use Muleta\Traits\Models\ValidatingTrait; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
12
|
|
|
|
|
13
|
|
|
class Datum extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
use ValidatingTrait; |
|
16
|
|
|
use CacheableEloquent; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $fillable = [ |
|
22
|
|
|
'session_id', |
|
23
|
|
|
'user_id', |
|
24
|
|
|
'user_type', |
|
25
|
|
|
'status_code', |
|
26
|
|
|
'uri', |
|
27
|
|
|
'method', |
|
28
|
|
|
'server', |
|
29
|
|
|
'input', |
|
30
|
|
|
'created_at', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $casts = [ |
|
37
|
|
|
'session_id' => 'string', |
|
38
|
|
|
'user_id' => 'integer', |
|
39
|
|
|
'user_type' => 'string', |
|
40
|
|
|
'status_code' => 'integer', |
|
41
|
|
|
'uri' => 'string', |
|
42
|
|
|
'method' => 'string', |
|
43
|
|
|
'server' => 'json', |
|
44
|
|
|
'input' => 'json', |
|
45
|
|
|
'created_at' => 'datetime', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public $timestamps = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $observables = [ |
|
57
|
|
|
'validating', |
|
58
|
|
|
'validated', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The default rules that the model will validate against. |
|
63
|
|
|
* |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
public $rules = [ |
|
67
|
|
|
'session_id' => 'required|string', |
|
68
|
|
|
'user_id' => 'nullable|integer', |
|
69
|
|
|
'user_type' => 'nullable|string', |
|
70
|
|
|
'status_code' => 'required|integer', |
|
71
|
|
|
'uri' => 'required|string', |
|
72
|
|
|
'method' => 'required|string', |
|
73
|
|
|
'server' => 'required|array', |
|
74
|
|
|
'input' => 'nullable|array', |
|
75
|
|
|
'created_at' => 'required|date', |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Whether the model should throw a |
|
80
|
|
|
* ValidationException if it fails validation. |
|
81
|
|
|
* |
|
82
|
|
|
* @var bool |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $throwValidationExceptions = true; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create a new Eloquent model instance. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $attributes |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct(array $attributes = []) |
|
92
|
|
|
{ |
|
93
|
|
|
parent::__construct($attributes); |
|
94
|
|
|
|
|
95
|
|
|
$this->setTable(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.data')); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the owning user. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
|
102
|
|
|
*/ |
|
103
|
|
|
public function user(): MorphTo |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->morphTo('user', 'user_type', 'user_id'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get bookings of the given user. |
|
110
|
|
|
* |
|
111
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
112
|
|
|
* @param \Illuminate\Database\Eloquent\Model $user |
|
113
|
|
|
* |
|
114
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
115
|
|
|
*/ |
|
116
|
|
|
public function scopeOfUser(Builder $builder, Model $user): Builder |
|
117
|
|
|
{ |
|
118
|
|
|
return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey()); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|