|null $data List of changes (old and new values). * @property Carbon $created_at The date on which the user was published. * * @property-read \App\Models\User $user User BelongsTo relation. * @property-read \App\Models\StaticPage $staticPage StaticPage BelongsTo relation. * @property-read \App\Models\Settings $settings Settings BelongsTo relation. * @property-read \App\Models\Range $range Range BelongsTo relation. * @property-read \App\Models\Product $product Product BelongsTo relation. * @property-read \App\Models\Prize $prize Prize BelongsTo relation. * @property-read \App\Models\Participation $participation Participation BelongsTo relation. * @property-read \App\Models\InstantWin $instantWin InstantWin BelongsTo relation. * @property-read \App\Models\FaqTheme $faqTheme FaqTheme BelongsTo relation. * @property-read \App\Models\FaqQuestion $faqQuestion FaqQuestion BelongsTo relation. * @property-read \App\Models\Customer $customer Customer BelongsTo relation. * @property-read \App\Models\Code $code Code BelongsTo relation. * @property-read \App\Models\BannedIp $bannedIp BannedIp BelongsTo relation. */ class ActivityLog extends Model { use HasFactory; use Notifiable; /** * Indicates if the model should be timestamped. * * @var boolean */ public $timestamps = false; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'is_anonymous', 'model_class', 'model_id', 'event', 'data', 'created_at', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'is_anonymous' => 'bool', 'is_console' => 'bool', 'event' => ActivityLogsEventEnum::class, 'data' => 'json', 'created_at' => 'datetime', ]; // * METHODS /** * Add new activity to the activity logs list. */ public static function addActivity(Model $model, ActivityLogsEventEnum $eventEnum): void { $isAnonymous = true; /** @var Model|null */ $userModel = auth('backend')->user(); $user_id = optional($userModel)->id; if ($userModel instanceof Model) { $user_id = $userModel->getKeyForSelectQuery(); $isAnonymous = false; } $activity = new self(); $activity->user_id = $user_id; $activity->is_console = app()->runningInConsole(); $activity->is_anonymous = $isAnonymous; $activity->model_class = $model::class; $activity->model_id = $model->getKey(); $activity->event = $eventEnum; $activity->data = static::getChangedColumns($activity, $model, $eventEnum); $activity->setAttribute('created_at', now()); $activity->saveOrFail(); } /** * Get old, new columns that has changed. * * @return array|null */ public static function getChangedColumns( self $activity, Model $model, ActivityLogsEventEnum $eventEnum ): ?array { if ($eventEnum !== ActivityLogsEventEnum::updated) { return null; } $casts = $model->getCasts(); $changes = $model->getChanges(); return collect($changes) ->map(fn($val, $key) => $casts[$key] ?? 'string') ->toArray(); } // * RELATIONS /** * User BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\User> */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * StaticPage BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\StaticPage> */ public function staticPage(): BelongsTo { return $this->belongsTo(StaticPage::class); } /** * Settings BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Settings> */ public function settings(): BelongsTo { return $this->belongsTo(Settings::class); } /** * Range BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Range> */ public function range(): BelongsTo { return $this->belongsTo(Range::class); } /** * Product BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Product> */ public function product(): BelongsTo { return $this->belongsTo(Product::class); } /** * Prize BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Prize> */ public function prize(): BelongsTo { return $this->belongsTo(Prize::class); } /** * Participation BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Participation> */ public function participation(): BelongsTo { return $this->belongsTo(Participation::class); } /** * InstantWin BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\InstantWin> */ public function instantWin(): BelongsTo { return $this->belongsTo(InstantWin::class); } /** * FaqTheme BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\FaqTheme> */ public function faqTheme(): BelongsTo { return $this->belongsTo(FaqTheme::class); } /** * FaqQuestion BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\FaqQuestion> */ public function faqQuestion(): BelongsTo { return $this->belongsTo(FaqQuestion::class); } /** * Customer BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Customer> */ public function customer(): BelongsTo { return $this->belongsTo(Customer::class); } /** * Code BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\Code> */ public function code(): BelongsTo { return $this->belongsTo(Code::class); } /** * BannedIp BelongsTo relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<$this,\App\Models\BannedIp> */ public function bannedIp(): BelongsTo { return $this->belongsTo(BannedIp::class); } }