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