Files
investbrain/app/Models/User.php
T

84 lines
2.3 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;
2025-01-28 17:14:49 -06:00
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
2024-08-01 13:53:10 -05:00
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
2025-01-28 17:14:49 -06:00
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
2024-08-22 22:45:17 -05:00
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
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;
2025-01-28 17:14:49 -06:00
use HasConnectedAccounts;
2024-08-01 13:53:10 -05:00
use HasFactory;
use HasProfilePhoto;
2025-01-28 17:14:49 -06:00
use HasRelationships;
use HasUuids;
2024-08-01 13:53:10 -05:00
use Notifiable;
use TwoFactorAuthenticatable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
2024-11-05 15:39:29 -06:00
'admin',
2024-08-01 13:53:10 -05:00
'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()
{
2024-10-21 22:23:20 -05:00
return $this->belongsToMany(Portfolio::class)->withPivot(['owner', 'full_access', 'invite_accepted_at']);
2024-08-01 13:53:10 -05:00
}
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()
2025-01-28 17:14:49 -06: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)
2025-01-28 17:14:49 -06:00
END AS gain_dollars');
2024-08-22 22:45:17 -05:00
}
2024-08-01 13:53:10 -05:00
}