Files
InvoiceShelf/app/Models/CompanySetting.php
Darko Gjorgjijoski 0fa1aac748 Add return types, typed parameters, and PHPDoc to all model methods
Modernize all 16 models with missing type declarations:
- Return types on ~87 methods (string, bool, void, array, mixed, etc.)
- Typed parameters where missing
- PHPDoc blocks on non-obvious methods explaining their purpose

Models updated: Invoice, Estimate, Payment, User, Company, Customer,
RecurringInvoice, Setting, CompanySetting, FileDisk, Transaction,
EmailLog, ExchangeRateLog, PaymentMethod, CustomField, CustomFieldValue.
2026-04-03 20:46:26 +02:00

81 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Collection;
class CompanySetting extends Model
{
use HasFactory;
protected $fillable = ['company_id', 'option', 'value'];
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
public function scopeWhereCompany($query, $company_id)
{
$query->where('company_id', $company_id);
}
/**
* Bulk create or update settings for a specific company.
*/
public static function setSettings(array $settings, mixed $company_id): void
{
foreach ($settings as $key => $value) {
self::updateOrCreate(
[
'option' => $key,
'company_id' => $company_id,
],
[
'option' => $key,
'company_id' => $company_id,
'value' => $value,
]
);
}
}
/**
* Retrieve all settings for a company as a key-value collection.
*/
public static function getAllSettings(mixed $company_id): Collection
{
return static::whereCompany($company_id)->get()->mapWithKeys(function ($item) {
return [$item['option'] => $item['value']];
});
}
/**
* Retrieve specific settings for a company as a key-value collection.
*/
public static function getSettings(array $settings, mixed $company_id): Collection
{
return static::whereIn('option', $settings)->whereCompany($company_id)
->get()->mapWithKeys(function ($item) {
return [$item['option'] => $item['value']];
});
}
/**
* Retrieve a single company setting value by key, or null if not found.
*/
public static function getSetting(string $key, mixed $company_id): mixed
{
$setting = static::whereOption($key)->whereCompany($company_id)->first();
if ($setting) {
return $setting->value;
} else {
return null;
}
}
}