wip
improve import / export flow and clean up relationships
This commit is contained in:
@@ -37,6 +37,15 @@ class DailyChange extends Model
|
||||
{
|
||||
return $query->where('portfolio_id', $portfolio);
|
||||
}
|
||||
|
||||
public function scopeMyDailyChanges()
|
||||
{
|
||||
return $this->whereHas('portfolio', function ($query) {
|
||||
$query->whereHas('users', function ($query) {
|
||||
$query->where('id', auth()->id());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function portfolio()
|
||||
{
|
||||
|
||||
@@ -56,7 +56,8 @@ class Holding extends Model
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol')
|
||||
->where('transactions.portfolio_id', $this->portfolio_id);
|
||||
->where('portfolio_id', $this->portfolio_id)
|
||||
->withAggregate('portfolio', 'title');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,7 +142,7 @@ class Holding extends Model
|
||||
|
||||
public function scopeSymbol($query, $symbol)
|
||||
{
|
||||
return $query->where('symbol', $symbol);
|
||||
return $query->where('holdings.symbol', $symbol);
|
||||
}
|
||||
|
||||
public function scopeWithoutWishlists($query) {
|
||||
|
||||
@@ -62,6 +62,13 @@ class Portfolio extends Model
|
||||
return $this->hasMany(DailyChange::class);
|
||||
}
|
||||
|
||||
public function scopeMyPortfolios()
|
||||
{
|
||||
return $this->whereHas('users', function ($query) {
|
||||
$query->where('user_id', auth()->user()->id);
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeWithoutWishlists()
|
||||
{
|
||||
return $this->where(['wishlist' => false]);
|
||||
|
||||
@@ -90,7 +90,6 @@ class Transaction extends Model
|
||||
public function scopeWithMarketData($query)
|
||||
{
|
||||
$query->withAggregate('market_data', 'name')
|
||||
->withAggregate('portfolio', 'title')
|
||||
->withAggregate('market_data', 'market_value')
|
||||
->withAggregate('market_data', 'fifty_two_week_low')
|
||||
->withAggregate('market_data', 'fifty_two_week_high')
|
||||
@@ -108,6 +107,15 @@ class Transaction extends Model
|
||||
return $query->where('symbol', $symbol);
|
||||
}
|
||||
|
||||
public function scopeMyTransactions()
|
||||
{
|
||||
return $this->whereHas('portfolio', function ($query) {
|
||||
$query->whereHas('users', function ($query) {
|
||||
$query->where('id', auth()->id());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function refreshMarketData()
|
||||
{
|
||||
return MarketData::getMarketData($this->attributes['symbol']);
|
||||
|
||||
@@ -72,6 +72,7 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class])
|
||||
->withMarketData()
|
||||
->withAggregate('portfolio', 'title')
|
||||
->selectRaw('
|
||||
CASE
|
||||
WHEN transaction_type = \'SELL\'
|
||||
|
||||
Reference in New Issue
Block a user