sfneal /
tracking
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Sfneal\Tracking\Models\Base; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Factories\Factory; |
||||
| 6 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||||
| 7 | use Sfneal\Models\Model; |
||||
| 8 | use Sfneal\Scopes\CreatedOrderScope; |
||||
| 9 | use Sfneal\Users\Models\User; |
||||
| 10 | |||||
| 11 | abstract class Tracking extends Model |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * The "booting" method of the model. |
||||
| 15 | * |
||||
| 16 | * @return void |
||||
| 17 | */ |
||||
| 18 | protected static function boot() |
||||
| 19 | { |
||||
| 20 | parent::boot(); |
||||
| 21 | static::addGlobalScope(new CreatedOrderScope()); |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * The attributes that should be mutated to dates. |
||||
| 26 | * |
||||
| 27 | * @var array |
||||
| 28 | */ |
||||
| 29 | protected $dates = ['deleted_at', 'updated_at', 'created_at']; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The attributes that should be cast to native types. |
||||
| 33 | * |
||||
| 34 | * @var array |
||||
| 35 | */ |
||||
| 36 | protected $casts = [ |
||||
| 37 | 'model_changes' => 'array', |
||||
| 38 | 'request_payload' => 'array', |
||||
| 39 | ]; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * @var array Attributes that should be appended to collections |
||||
| 43 | */ |
||||
| 44 | protected $appends = [ |
||||
| 45 | 'has_model_changes', |
||||
| 46 | ]; |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * Create a new factory instance for the model. |
||||
| 50 | * |
||||
| 51 | * @return Factory |
||||
| 52 | */ |
||||
| 53 | abstract protected static function newFactory(); |
||||
| 54 | |||||
| 55 | /** |
||||
| 56 | * User relationship. |
||||
| 57 | * |
||||
| 58 | * @return BelongsTo|User |
||||
| 59 | */ |
||||
| 60 | public function user() |
||||
| 61 | { |
||||
| 62 | return $this->belongsTo(User::class, 'user_id', 'id'); |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Mutate date into human readable format. |
||||
| 67 | * |
||||
| 68 | * @return false|string |
||||
| 69 | */ |
||||
| 70 | public function getDateAttribute() |
||||
| 71 | { |
||||
| 72 | return date('Y-m-d', strtotime($this->created_at)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Mutate time into human readable format. |
||||
| 77 | * |
||||
| 78 | * @return false|string |
||||
| 79 | */ |
||||
| 80 | public function getTimeAttribute() |
||||
| 81 | { |
||||
| 82 | return date('h:i a', strtotime($this->created_at)); |
||||
|
0 ignored issues
–
show
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * Mutate model changes array to remove any dates attributes before display. |
||||
| 87 | * |
||||
| 88 | * @param $value |
||||
| 89 | * @return array |
||||
| 90 | */ |
||||
| 91 | public function getModelChangesAttribute($value) |
||||
| 92 | { |
||||
| 93 | // Remove $dates from returned array |
||||
| 94 | if (isset($value) && ! empty($value)) { |
||||
| 95 | return array_filter(json_decode($value, true), function ($val) { |
||||
| 96 | return ! in_array($val, $this->dates); |
||||
| 97 | }, ARRAY_FILTER_USE_KEY); |
||||
| 98 | } else { |
||||
| 99 | return []; |
||||
| 100 | } |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * Determine if an activity or action instance has model_changes. |
||||
| 105 | * |
||||
| 106 | * @return bool |
||||
| 107 | */ |
||||
| 108 | public function getHasModelChangesAttribute() |
||||
| 109 | { |
||||
| 110 | return count($this->model_changes) > 0; |
||||
| 111 | } |
||||
| 112 | } |
||||
| 113 |