<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Setting extends Model {

    public $timestamps = false;

    public static function get(String $settingKey, $default = null) {
        return self::where('key', $settingKey)->firstOr(function () use ($settingKey, $default) {
            $setting = new Setting();
            $setting->key = $settingKey;
            $setting->value = $default;
            return $setting;
        })->value;
    }

    public static function insertOrUpdate(array $rows) {

        $values = "";
        foreach ($rows as $key => $value) {
            $values .= "('$key', '$value'),";
        }

        $values = rtrim($values, ",");
        $sql = "REPLACE INTO `settings` (`key`,`value`) VALUES {$values}";

        return DB::statement($sql);
    }
}
