improve naming for query scopes
This commit is contained in:
@@ -22,7 +22,7 @@ class DashboardController extends Controller
|
||||
return
|
||||
Holding::query()
|
||||
->myHoldings()
|
||||
->getPortfolioMetrics()
|
||||
->withPortfolioMetrics()
|
||||
->first();
|
||||
}
|
||||
);
|
||||
|
||||
@@ -15,13 +15,13 @@ class HoldingController extends Controller
|
||||
public function show(Request $request, Portfolio $portfolio, String $symbol)
|
||||
{
|
||||
|
||||
$holding = Holding::with(['market_data'])
|
||||
$holding = Holding::with(['market_data', 'transactions'])
|
||||
->symbol($symbol)
|
||||
->portfolio($portfolio->id)
|
||||
->firstOrFail();
|
||||
|
||||
$transactions = $holding->transactions;
|
||||
// $transactions = dd($holding->transactions()->toSql());
|
||||
|
||||
return view('holding.show', compact(['portfolio', 'holding', 'transactions']));
|
||||
return view('holding.show', compact(['portfolio', 'holding']));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class PortfolioController extends Controller
|
||||
function () use ($portfolio) {
|
||||
return Holding::query()
|
||||
->portfolio($portfolio->id)
|
||||
->getPortfolioMetrics()
|
||||
->withPortfolioMetrics()
|
||||
->first();
|
||||
}
|
||||
);
|
||||
|
||||
+10
-6
@@ -55,9 +55,7 @@ class Holding extends Model
|
||||
*/
|
||||
public function transactions()
|
||||
{
|
||||
|
||||
return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id')
|
||||
->where('symbol', $this->symbol);
|
||||
return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,6 +133,13 @@ class Holding extends Model
|
||||
->join('market_data', 'holdings.symbol', 'market_data.symbol');
|
||||
}
|
||||
|
||||
public function scopeWithPerformance($query)
|
||||
{
|
||||
return $query->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
||||
->selectRaw('COALESCE((market_data.market_value - holdings.average_cost_basis) * holdings.quantity, 0) AS market_gain_dollars')
|
||||
->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent');
|
||||
}
|
||||
|
||||
public function scopePortfolio($query, $portfolio)
|
||||
{
|
||||
return $query->where('portfolio_id', $portfolio);
|
||||
@@ -157,10 +162,9 @@ class Holding extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeGetPortfolioMetrics($query)
|
||||
public function scopeWithPortfolioMetrics($query)
|
||||
{
|
||||
|
||||
$query->selectRaw('COALESCE(SUM(holdings.dividends_earned),0) AS total_dividends_earned')
|
||||
return $query->selectRaw('COALESCE(SUM(holdings.dividends_earned),0) AS total_dividends_earned')
|
||||
->selectRaw('COALESCE(SUM(holdings.realized_gain_dollars),0) AS realized_gain_dollars')
|
||||
->selectRaw('@total_market_value:=COALESCE(SUM(holdings.quantity * market_data.market_value),0) AS total_market_value')
|
||||
->selectRaw('@sum_total_cost_basis:=COALESCE(SUM(holdings.total_cost_basis),0) AS total_cost_basis')
|
||||
|
||||
@@ -44,9 +44,7 @@ class Portfolio extends Model
|
||||
{
|
||||
return $this->hasMany(Holding::class, 'portfolio_id')
|
||||
->withMarketData()
|
||||
->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
||||
->selectRaw('COALESCE((market_data.market_value - holdings.average_cost_basis) * holdings.quantity, 0) AS market_gain_dollars')
|
||||
->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent');
|
||||
->withPerformance();
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
|
||||
+1
-3
@@ -63,9 +63,7 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
|
||||
->withMarketData()
|
||||
->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
||||
->selectRaw('COALESCE((market_data.market_value - holdings.average_cost_basis) * holdings.quantity, 0) AS market_gain_dollars')
|
||||
->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent');
|
||||
->withPerformance();
|
||||
}
|
||||
|
||||
public function transactions(): HasManyDeep
|
||||
|
||||
Reference in New Issue
Block a user