Configuration.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\Cache;
  6. class Configuration extends Model
  7. {
  8. use HasFactory;
  9. public const CACHE_TAG = 'configuration';
  10. public $primaryKey = 'key';
  11. public $incrementing = false;
  12. protected $keyType = 'string';
  13. protected $fillable = [
  14. 'key',
  15. 'value',
  16. 'type',
  17. ];
  18. public static function boot()
  19. {
  20. parent::boot();
  21. static::updated(function (Configuration $configuration) {
  22. Cache::forget(self::CACHE_TAG .':'. $configuration->key);
  23. });
  24. }
  25. /**
  26. * @param string $key
  27. * @param $default
  28. * @return mixed
  29. */
  30. public static function getValueByKey(string $key, $default = null)
  31. {
  32. return Cache::rememberForever(self::CACHE_TAG .':'. $key, function () use ($default, $key) {
  33. $configuration = self::find($key);
  34. return $configuration ? $configuration->value : $default;
  35. });
  36. }
  37. }