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
|
||||
|
||||
@@ -114,7 +114,7 @@
|
||||
|
||||
@livewire('transactions-list', [
|
||||
'portfolio' => $holding->portfolio,
|
||||
'transactions' => $transactions,
|
||||
'transactions' => $holding->transactions,
|
||||
'shouldGoToHolding' => false
|
||||
])
|
||||
|
||||
|
||||
@@ -39,14 +39,16 @@ new class extends Component {
|
||||
|
||||
public function holdings(): Collection
|
||||
{
|
||||
return $this->portfolio
|
||||
->holdings()
|
||||
->withCount(['transactions as num_transactions' => function ($query) {
|
||||
$query->where('portfolio_id', $this->portfolio->id);
|
||||
}])
|
||||
->orderBy(...array_values($this->sortBy))
|
||||
->where('quantity', '>', 0)
|
||||
->get();
|
||||
$holdings = $this->portfolio
|
||||
->holdings()
|
||||
->withCount(['transactions as num_transactions' => function($query) {
|
||||
return $query->whereRaw('transactions.symbol = holdings.symbol');
|
||||
}])
|
||||
->orderBy(...array_values($this->sortBy))
|
||||
->where('holdings.quantity', '>', 0)
|
||||
->get();
|
||||
|
||||
return $holdings;
|
||||
}
|
||||
|
||||
public function goToHolding($holding)
|
||||
|
||||
@@ -52,7 +52,7 @@ new class extends Component {
|
||||
// $key = 'portfolio-metrics-' . $portfolio->id;
|
||||
// $metrics = cache()->remember($key, 60, function () use ($portfolio) {
|
||||
// return Holding::where(['portfolio_id' => $portfolio->id])
|
||||
// ->getPortfolioMetrics()
|
||||
// ->withPortfolioMetrics()
|
||||
// ->first();
|
||||
// });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user