Setting.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\Model;
  4. use Prettus\Repository\Contracts\Transformable;
  5. use Prettus\Repository\Traits\TransformableTrait;
  6. /**
  7. * Class Setting.
  8. *
  9. * @package namespace App\Repositories\Models\Base;
  10. */
  11. class Setting extends Model implements Transformable
  12. {
  13. use TransformableTrait;
  14. protected $table = 'base_settings';
  15. /**
  16. * The attributes that are mass assignable.
  17. *
  18. * @var array
  19. */
  20. protected $guarded = [];
  21. public static function byKeyGetOptions($key)
  22. {
  23. return self::query()->where('parent_id', self::query()->where('key', $key)->value('id'))->pluck('name', 'id');
  24. }
  25. public static function byKey($key)
  26. {
  27. // return Cache::remember("cache:model:setting:bykey:" . $key, Carbon::now()->addHours(1), function () use ($key) {
  28. return Setting::query()->where('key', $key)->value('value');
  29. // });
  30. }
  31. public static function byIdGetName($id)
  32. {
  33. return self::query()->where('id', $id)->where('parent_id', '>', 0)->value('name');
  34. }
  35. public function setKeyAttribute($val)
  36. {
  37. $this->attributes['key'] = strtoupper($val);
  38. }
  39. public function parent()
  40. {
  41. return $this->belongsTo(self::class, 'parent_id', 'id');
  42. }
  43. public function getParentNameAttribute()
  44. {
  45. return self::query()->where('id', $this->attributes['parent_id'])->value('name');
  46. }
  47. public function getValueAttribute($val)
  48. {
  49. if (!isset($this->attributes['type'])) {
  50. return $val;
  51. }
  52. $type = $this->attributes['type'];
  53. switch ($type) {
  54. case 0:
  55. return $val;
  56. break;
  57. case 1:
  58. return Resource::query()->where('id', $val)->select(['id', 'url', 'path'])->first();
  59. break;
  60. }
  61. return $val;
  62. }
  63. }