Files
InvoiceShelf/app/Models/CustomField.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

108 lines
2.4 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\Database\Eloquent\Relations\HasMany;
class CustomField extends Model
{
use HasFactory;
protected $guarded = [
'id',
];
protected $dates = [
'date_answer',
'date_time_answer',
];
protected $appends = [
'defaultAnswer',
];
protected function casts(): array
{
return [
'options' => 'array',
];
}
public function setTimeAnswerAttribute(mixed $value): void
{
if ($value && $value != null) {
$this->attributes['time_answer'] = date('H:i:s', strtotime($value));
}
}
public function setOptionsAttribute(mixed $value): void
{
$this->attributes['options'] = json_encode($value);
}
public function getDefaultAnswerAttribute()
{
$value_type = getCustomFieldValueKey($this->type);
return $this->$value_type;
}
public function getInUseAttribute()
{
return $this->customFieldValues()->exists();
}
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
public function customFieldValues(): HasMany
{
return $this->hasMany(CustomFieldValue::class);
}
public function scopeWhereCompany($query)
{
return $query->where('custom_fields.company_id', request()->header('company'));
}
public function scopeWhereSearch($query, $search)
{
$query->where(function ($query) use ($search) {
$query->where('label', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%');
});
}
public function scopePaginateData($query, $limit)
{
if ($limit == 'all') {
return $query->get();
}
return $query->paginate($limit);
}
public function scopeApplyFilters($query, array $filters)
{
$filters = collect($filters);
if ($filters->get('type')) {
$query->whereType($filters->get('type'));
}
if ($filters->get('search')) {
$query->whereSearch($filters->get('search'));
}
}
public function scopeWhereType($query, $type)
{
$query->where('custom_fields.model_type', $type);
}
}