['required', 'between:1,100', 'alphabasic'], 'user_id' => 'required|exists:users,id', 'account_type_id' => 'required|exists:account_types,id', 'active' => 'required|boolean' ]; /** * Account type. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function accountType() { return $this->belongsTo('AccountType'); } /** * Get an accounts current balance. * * @param \Carbon\Carbon $date * * @return float */ public function balance(\Carbon\Carbon $date = null) { $date = is_null($date) ? new \Carbon\Carbon : $date; return floatval( $this->transactions() ->leftJoin( 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' ) ->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount') ); } /** * Transactions. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function transactions() { return $this->hasMany('Transaction'); } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function piggybanks() { return $this->hasMany('Piggybank'); } /** * @param \Carbon\Carbon $date * * @return null */ public function predict( /** @noinspection PhpUnusedParameterInspection */ \Carbon\Carbon $date ) { return null; } /** * User * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user() { return $this->belongsTo('User'); } public function scopeAccountType(Builder $query, array $types) { if(is_null($this->joinedAccountTypes)) { $query->leftJoin('account_types','account_types.id','=','accounts.account_type_id'); $this->joinedAccountTypes = true; } $query->whereIn('account_types.type',$types); } }