Setting.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. public const KEY_TTN_NEXT_NUMBER = 'ttn_next_number';
  9. protected $fillable = [
  10. 'key',
  11. 'value',
  12. ];
  13. public static function get(string $key, mixed $default = null): mixed
  14. {
  15. $value = static::query()->where('key', $key)->value('value');
  16. return $value ?? $default;
  17. }
  18. public static function getInt(string $key, ?int $default = null): ?int
  19. {
  20. $value = static::get($key, $default);
  21. if ($value === null || $value === '') {
  22. return $default;
  23. }
  24. return (int) $value;
  25. }
  26. public static function set(string $key, mixed $value): void
  27. {
  28. static::query()->updateOrCreate(
  29. ['key' => $key],
  30. ['value' => $value === null ? null : (string) $value]
  31. );
  32. }
  33. }