Setting.php 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Setting extends Model
  5. {
  6. public const KEY_DEFAULT_MAF_ORDER_USER_ID = 'default_maf_order_user_id';
  7. public const KEY_RECLAMATION_ACT_REPRESENTATIVE_USER_ID = 'reclamation_act_representative_user_id';
  8. protected $fillable = [
  9. 'key',
  10. 'value',
  11. ];
  12. public static function get(string $key, mixed $default = null): mixed
  13. {
  14. $value = static::query()->where('key', $key)->value('value');
  15. return $value ?? $default;
  16. }
  17. public static function getInt(string $key, ?int $default = null): ?int
  18. {
  19. $value = static::get($key, $default);
  20. if ($value === null || $value === '') {
  21. return $default;
  22. }
  23. return (int) $value;
  24. }
  25. public static function set(string $key, mixed $value): void
  26. {
  27. static::query()->updateOrCreate(
  28. ['key' => $key],
  29. ['value' => $value === null ? null : (string) $value]
  30. );
  31. }
  32. }