. */ declare(strict_types=1); namespace FireflyIII\Models; use FireflyIII\Handlers\Observer\BudgetObserver; use FireflyIII\Support\Models\ReturnsIntegerIdTrait; use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait; use FireflyIII\User; use Illuminate\Database\Eloquent\Attributes\ObservedBy; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; #[ObservedBy([BudgetObserver::class])] class Budget extends Model { use ReturnsIntegerIdTrait; use ReturnsIntegerUserIdTrait; use SoftDeletes; protected $fillable = ['user_id', 'user_group_id', 'name', 'active', 'order', 'user_group_id']; protected $hidden = ['encrypted']; /** * Route binder. Converts the key in the URL to the specified object (or throw 404). * * @throws NotFoundHttpException */ public static function routeBinder(string $value): self { if (auth()->check()) { $budgetId = (int)$value; /** @var User $user */ $user = auth()->user(); /** @var null|Budget $budget */ $budget = $user->budgets()->find($budgetId); if (null !== $budget) { return $budget; } } throw new NotFoundHttpException(); } public function user(): BelongsTo { return $this->belongsTo(User::class); } public function attachments(): MorphMany { return $this->morphMany(Attachment::class, 'attachable'); } public function autoBudgets(): HasMany { return $this->hasMany(AutoBudget::class); } public function budgetlimits(): HasMany { return $this->hasMany(BudgetLimit::class); } /** * Get all of the notes. */ public function notes(): MorphMany { return $this->morphMany(Note::class, 'noteable'); } public function transactionJournals(): BelongsToMany { return $this->belongsToMany(TransactionJournal::class, 'budget_transaction_journal', 'budget_id'); } public function transactions(): BelongsToMany { return $this->belongsToMany(Transaction::class, 'budget_transaction', 'budget_id'); } protected function casts(): array { return [ 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', 'active' => 'boolean', 'encrypted' => 'boolean', 'user_id' => 'integer', 'user_group_id' => 'integer', ]; } protected function order(): Attribute { return Attribute::make( get: static fn($value) => (int)$value, ); } }