belongsTo(Company::class); } /** * Narrow a query to one company's rows. */ public function scopeWhereCompany($query, $company_id) { $query->where('company_id', $company_id); } /** * Write a batch of preferences for one company, replacing the value of any * option already on file and inserting the rest. */ public static function setSettings(array $settings, mixed $company_id): void { foreach ($settings as $option => $value) { self::updateOrCreate( ['option' => $option, 'company_id' => $company_id], ['option' => $option, 'company_id' => $company_id, 'value' => $value] ); } } /** * Every preference on file for a company, keyed by option name. */ public static function getAllSettings(mixed $company_id): Collection { return self::flatten( static::whereCompany($company_id)->get() ); } /** * The named preferences only; options with no row on file are left out. */ public static function getSettings(array $settings, mixed $company_id): Collection { return self::flatten( static::whereIn('option', $settings)->whereCompany($company_id)->get() ); } /** * One preference value, or null when the company has no row for it. */ public static function getSetting(string $key, mixed $company_id): mixed { $setting = static::query() ->where('option', $key) ->whereCompany($company_id) ->first(); if ($setting) { return $setting->value; } else { return null; } } /** * Reduce preference rows to an option => value collection. */ private static function flatten(Collection $rows): Collection { return $rows->mapWithKeys(function ($row) { return [$row['option'] => $row['value']]; }); } }