improve naming for query scopes
This commit is contained in:
@@ -22,7 +22,7 @@ class DashboardController extends Controller
|
|||||||
return
|
return
|
||||||
Holding::query()
|
Holding::query()
|
||||||
->myHoldings()
|
->myHoldings()
|
||||||
->getPortfolioMetrics()
|
->withPortfolioMetrics()
|
||||||
->first();
|
->first();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -15,13 +15,13 @@ class HoldingController extends Controller
|
|||||||
public function show(Request $request, Portfolio $portfolio, String $symbol)
|
public function show(Request $request, Portfolio $portfolio, String $symbol)
|
||||||
{
|
{
|
||||||
|
|
||||||
$holding = Holding::with(['market_data'])
|
$holding = Holding::with(['market_data', 'transactions'])
|
||||||
->symbol($symbol)
|
->symbol($symbol)
|
||||||
->portfolio($portfolio->id)
|
->portfolio($portfolio->id)
|
||||||
->firstOrFail();
|
->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) {
|
function () use ($portfolio) {
|
||||||
return Holding::query()
|
return Holding::query()
|
||||||
->portfolio($portfolio->id)
|
->portfolio($portfolio->id)
|
||||||
->getPortfolioMetrics()
|
->withPortfolioMetrics()
|
||||||
->first();
|
->first();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
+10
-6
@@ -55,9 +55,7 @@ class Holding extends Model
|
|||||||
*/
|
*/
|
||||||
public function transactions()
|
public function transactions()
|
||||||
{
|
{
|
||||||
|
return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id');
|
||||||
return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id')
|
|
||||||
->where('symbol', $this->symbol);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -135,6 +133,13 @@ class Holding extends Model
|
|||||||
->join('market_data', 'holdings.symbol', 'market_data.symbol');
|
->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)
|
public function scopePortfolio($query, $portfolio)
|
||||||
{
|
{
|
||||||
return $query->where('portfolio_id', $portfolio);
|
return $query->where('portfolio_id', $portfolio);
|
||||||
@@ -157,10 +162,9 @@ class Holding extends Model
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeGetPortfolioMetrics($query)
|
public function scopeWithPortfolioMetrics($query)
|
||||||
{
|
{
|
||||||
|
return $query->selectRaw('COALESCE(SUM(holdings.dividends_earned),0) AS total_dividends_earned')
|
||||||
$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('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('@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')
|
->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')
|
return $this->hasMany(Holding::class, 'portfolio_id')
|
||||||
->withMarketData()
|
->withMarketData()
|
||||||
->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
->withPerformance();
|
||||||
->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 transactions()
|
public function transactions()
|
||||||
|
|||||||
+1
-3
@@ -63,9 +63,7 @@ class User extends Authenticatable
|
|||||||
{
|
{
|
||||||
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
|
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
|
||||||
->withMarketData()
|
->withMarketData()
|
||||||
->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
->withPerformance();
|
||||||
->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 transactions(): HasManyDeep
|
public function transactions(): HasManyDeep
|
||||||
|
|||||||
@@ -114,7 +114,7 @@
|
|||||||
|
|
||||||
@livewire('transactions-list', [
|
@livewire('transactions-list', [
|
||||||
'portfolio' => $holding->portfolio,
|
'portfolio' => $holding->portfolio,
|
||||||
'transactions' => $transactions,
|
'transactions' => $holding->transactions,
|
||||||
'shouldGoToHolding' => false
|
'shouldGoToHolding' => false
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|||||||
@@ -39,14 +39,16 @@ new class extends Component {
|
|||||||
|
|
||||||
public function holdings(): Collection
|
public function holdings(): Collection
|
||||||
{
|
{
|
||||||
return $this->portfolio
|
$holdings = $this->portfolio
|
||||||
->holdings()
|
->holdings()
|
||||||
->withCount(['transactions as num_transactions' => function ($query) {
|
->withCount(['transactions as num_transactions' => function($query) {
|
||||||
$query->where('portfolio_id', $this->portfolio->id);
|
return $query->whereRaw('transactions.symbol = holdings.symbol');
|
||||||
}])
|
}])
|
||||||
->orderBy(...array_values($this->sortBy))
|
->orderBy(...array_values($this->sortBy))
|
||||||
->where('quantity', '>', 0)
|
->where('holdings.quantity', '>', 0)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
return $holdings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function goToHolding($holding)
|
public function goToHolding($holding)
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ new class extends Component {
|
|||||||
// $key = 'portfolio-metrics-' . $portfolio->id;
|
// $key = 'portfolio-metrics-' . $portfolio->id;
|
||||||
// $metrics = cache()->remember($key, 60, function () use ($portfolio) {
|
// $metrics = cache()->remember($key, 60, function () use ($portfolio) {
|
||||||
// return Holding::where(['portfolio_id' => $portfolio->id])
|
// return Holding::where(['portfolio_id' => $portfolio->id])
|
||||||
// ->getPortfolioMetrics()
|
// ->withPortfolioMetrics()
|
||||||
// ->first();
|
// ->first();
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user