|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Tracking\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Database\Factories\TrackActivityFactory; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
9
|
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope; |
|
10
|
|
|
use Sfneal\Scopes\CreatedOrderScope; |
|
11
|
|
|
use Sfneal\Scopes\IdOrderScope; |
|
12
|
|
|
use Sfneal\Tracking\Builders\TrackActivityBuilder; |
|
13
|
|
|
use Sfneal\Tracking\Models\Base\Tracking; |
|
14
|
|
|
use Sfneal\Tracking\Models\Traits\TrackingRelationships; |
|
15
|
|
|
|
|
16
|
|
|
class TrackActivity extends Tracking |
|
17
|
|
|
{ |
|
18
|
|
|
// todo: add use of polymorphic relationships |
|
19
|
|
|
use HasFactory; |
|
20
|
|
|
use TrackingRelationships; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The "booting" method of the model. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static function boot() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::boot(); |
|
30
|
|
|
static::addGlobalScope(new CreatedOrderScope()); |
|
31
|
|
|
static::addGlobalScope(new IdOrderScope()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected $table = 'track_activity'; |
|
35
|
|
|
protected $primaryKey = 'track_activity_id'; |
|
36
|
|
|
|
|
37
|
|
|
protected $fillable = [ |
|
38
|
|
|
'track_activity_id', |
|
39
|
|
|
'user_id', |
|
40
|
|
|
'route', |
|
41
|
|
|
'description', |
|
42
|
|
|
'model_table', |
|
43
|
|
|
'model_key', |
|
44
|
|
|
'model_changes', |
|
45
|
|
|
'request_token', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The attributes that should be cast to native types. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $casts = [ |
|
54
|
|
|
'user_id' => 'int', |
|
55
|
|
|
'model_key' => 'int', |
|
56
|
|
|
'model_changes' => 'array', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create a new factory instance for the model. |
|
61
|
|
|
* |
|
62
|
|
|
* @return TrackActivityFactory |
|
63
|
|
|
*/ |
|
64
|
|
|
protected static function newFactory(): TrackActivityFactory |
|
65
|
|
|
{ |
|
66
|
|
|
return new TrackActivityFactory(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Query Builder. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $query |
|
73
|
|
|
* |
|
74
|
|
|
* @return TrackActivityBuilder |
|
75
|
|
|
*/ |
|
76
|
|
|
public function newEloquentBuilder($query) |
|
77
|
|
|
{ |
|
78
|
|
|
return new TrackActivityBuilder($query); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return TrackActivityBuilder|Builder |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function query(): TrackActivityBuilder |
|
85
|
|
|
{ |
|
86
|
|
|
return parent::query(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Related TrackTraffic data. |
|
91
|
|
|
* |
|
92
|
|
|
* @return BelongsTo |
|
93
|
|
|
*/ |
|
94
|
|
|
public function tracking() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->belongsTo(TrackTraffic::class, 'request_token', 'request_token') |
|
97
|
|
|
->withoutGlobalScope(SoftDeletingScope::class); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Determine if the Activity has model changes or a request payload. |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getHasModelChangesAttribute() |
|
106
|
|
|
{ |
|
107
|
|
|
return parent::getHasModelChangesAttribute() || $this->hasTrackingLoadedWithPayload(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Determine if the 'tracking' relationship has been eager loaded and that it has a request_payload. |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function hasTrackingLoadedWithPayload() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->relationLoaded('tracking') && isset($this->tracking->request_payload); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|