12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Repositories\Models\Base;
- use App\Repositories\Models\Model;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class Setting.
- *
- * @package namespace App\Repositories\Models\Base;
- */
- class Setting extends Model implements Transformable
- {
- use TransformableTrait;
- protected $table = 'base_settings';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- public static function byKeyGetOptions($key)
- {
- return self::query()->where('parent_id', self::query()->where('key', $key)->value('id'))->pluck('name', 'id');
- }
- public static function byKey($key)
- {
- // return Cache::remember("cache:model:setting:bykey:" . $key, Carbon::now()->addHours(1), function () use ($key) {
- return Setting::query()->where('key', $key)->value('value');
- // });
- }
- public static function byIdGetName($id)
- {
- return self::query()->where('id', $id)->where('parent_id', '>', 0)->value('name');
- }
- public function setKeyAttribute($val)
- {
- $this->attributes['key'] = strtoupper($val);
- }
- public function parent()
- {
- return $this->belongsTo(self::class, 'parent_id', 'id');
- }
- public function getParentNameAttribute()
- {
- return self::query()->where('id', $this->attributes['parent_id'])->value('name');
- }
- public function getValueAttribute($val)
- {
- if (!isset($this->attributes['type'])) {
- return $val;
- }
- $type = $this->attributes['type'];
- switch ($type) {
- case 0:
- return $val;
- break;
- case 1:
- return Resource::query()->where('id', $val)->select(['id', 'url', 'path'])->first();
- break;
- }
- return $val;
- }
- }
|