Files
investbrain/app/Models/User.php
T

83 lines
2.2 KiB
PHP
Raw Normal View History

2024-08-01 13:53:10 -05:00
<?php
namespace App\Models;
2024-10-19 23:11:04 -05:00
use App\Traits\HasConnectedAccounts;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Jetstream\HasProfilePhoto;
2024-08-01 13:53:10 -05:00
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
2024-08-22 22:45:17 -05:00
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
2024-08-22 22:45:17 -05:00
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
2024-10-19 23:11:04 -05:00
use Illuminate\Contracts\Auth\MustVerifyEmail;
2024-08-01 13:53:10 -05:00
2024-10-19 23:11:04 -05:00
class User extends Authenticatable implements MustVerifyEmail
2024-08-01 13:53:10 -05:00
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use HasUuids;
2024-08-22 22:45:17 -05:00
use HasRelationships;
2024-10-19 23:11:04 -05:00
use HasConnectedAccounts;
2024-08-01 13:53:10 -05:00
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
protected $appends = [
'profile_photo_url',
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function portfolios()
{
return $this->belongsToMany(Portfolio::class)->withPivot('owner');
}
2024-08-27 22:06:10 -05:00
public function daily_changes()
{
return $this->hasManyDeep(DailyChange::class, ['portfolio_user', Portfolio::class]);
}
2024-08-22 22:45:17 -05:00
public function holdings(): HasManyDeep
{
2024-08-24 22:51:45 -05:00
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
2024-08-28 15:19:20 -05:00
->withMarketData()
2024-08-29 18:46:21 -05:00
->withPerformance();
2024-08-22 22:45:17 -05:00
}
public function transactions(): HasManyDeep
{
2024-08-26 19:49:43 -05:00
return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class])
2024-08-28 15:19:20 -05:00
->withMarketData()
2024-08-28 22:06:47 -05:00
->withAggregate('portfolio', 'title')
2024-08-27 21:17:54 -05:00
->selectRaw('
CASE
WHEN transaction_type = \'SELL\'
THEN COALESCE(transactions.sale_price - transactions.cost_basis, 0)
ELSE COALESCE(market_data.market_value - transactions.cost_basis, 0)
2024-08-28 15:19:20 -05:00
END AS gain_dollars');
2024-08-22 22:45:17 -05:00
}
2024-08-01 13:53:10 -05:00
}