<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Setting extends Model
{
    use HasFactory;
    protected $fillable = ['key', 'value', 'display_name', 'type', 'group'];

    protected static function booted()
    {
        static::created(function ($user) {
            static::removeFromCache();
        });
        static::updated(function ($user) {
            static::removeFromCache();
        });
        static::deleted(function ($user) {
            static::removeFromCache();
        });
    }

    public static function removeFromCache()
    {
        Cache::forget('settings');
    }

    public function scopeSetting($query, $key)
    {
        return $query->where('key', $key);
    }
}
