This commit is contained in:
hackerESQ
2024-08-28 23:32:01 -05:00
parent 69c43dc41f
commit 1e0a149ded
6 changed files with 28 additions and 25 deletions
+8 -6
View File
@@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Holding;
use App\Models\Portfolio; use App\Models\Portfolio;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -14,12 +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 = $portfolio->holdings() $holding = Holding::with(['market_data'])
->with(['market_data']) ->symbol($symbol)
->symbol($symbol) ->portfolio($portfolio->id)
->portfolio($portfolio->id) ->firstOrFail();
->first();
return view('holding.show', compact(['portfolio', 'holding'])); $transactions = $holding->transactions;
return view('holding.show', compact(['portfolio', 'holding', 'transactions']));
} }
} }
+9 -9
View File
@@ -55,9 +55,9 @@ class Holding extends Model
*/ */
public function transactions() public function transactions()
{ {
return $this->hasMany(Transaction::class, 'symbol', 'symbol')
->where('portfolio_id', $this->portfolio_id) return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id')
->withAggregate('portfolio', 'title'); ->where('symbol', $this->symbol);
} }
/** /**
@@ -127,12 +127,12 @@ class Holding extends Model
public function scopeWithMarketData($query) public function scopeWithMarketData($query)
{ {
$query->withAggregate('market_data', 'name') return $query->withAggregate('market_data', 'name')
->withAggregate('market_data', 'market_value') ->withAggregate('market_data', 'market_value')
->withAggregate('market_data', 'fifty_two_week_low') ->withAggregate('market_data', 'fifty_two_week_low')
->withAggregate('market_data', 'fifty_two_week_high') ->withAggregate('market_data', 'fifty_two_week_high')
->withAggregate('market_data', 'updated_at') ->withAggregate('market_data', 'updated_at')
->join('market_data', 'holdings.symbol', 'market_data.symbol'); ->join('market_data', 'holdings.symbol', 'market_data.symbol');
} }
public function scopePortfolio($query, $portfolio) public function scopePortfolio($query, $portfolio)
+5 -8
View File
@@ -42,14 +42,11 @@ class Portfolio extends Model
public function holdings() public function holdings()
{ {
return $this->hasMany(Holding::class, 'portfolio_id', 'id') return $this->hasMany(Holding::class, 'portfolio_id')
->withCount(['transactions as num_transactions' => function ($query) { ->withMarketData()
$query->portfolio($this->id); ->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')
->withMarketData() ->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent');
->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 transactions() public function transactions()
+1 -1
View File
@@ -89,7 +89,7 @@ class Transaction extends Model
public function scopeWithMarketData($query) public function scopeWithMarketData($query)
{ {
$query->withAggregate('market_data', 'name') return $query->withAggregate('market_data', 'name')
->withAggregate('market_data', 'market_value') ->withAggregate('market_data', 'market_value')
->withAggregate('market_data', 'fifty_two_week_low') ->withAggregate('market_data', 'fifty_two_week_low')
->withAggregate('market_data', 'fifty_two_week_high') ->withAggregate('market_data', 'fifty_two_week_high')
+1 -1
View File
@@ -114,7 +114,7 @@
@livewire('transactions-list', [ @livewire('transactions-list', [
'portfolio' => $holding->portfolio, 'portfolio' => $holding->portfolio,
'transactions' => $holding->transactions, 'transactions' => $transactions,
'shouldGoToHolding' => false 'shouldGoToHolding' => false
]) ])
@@ -1,5 +1,6 @@
<?php <?php
use App\Models\Holding;
use App\Models\Portfolio; use App\Models\Portfolio;
use App\Models\Transaction; use App\Models\Transaction;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -40,6 +41,9 @@ new class extends Component {
{ {
return $this->portfolio return $this->portfolio
->holdings() ->holdings()
->withCount(['transactions as num_transactions' => function ($query) {
$query->where('portfolio_id', $this->portfolio->id);
}])
->orderBy(...array_values($this->sortBy)) ->orderBy(...array_values($this->sortBy))
->where('quantity', '>', 0) ->where('quantity', '>', 0)
->get(); ->get();