Add market data scope

This commit is contained in:
hackerESQ
2024-08-28 15:19:20 -05:00
parent 8a136fe1d7
commit e684c1f4a3
4 changed files with 27 additions and 27 deletions
+10 -5
View File
@@ -16,8 +16,6 @@ class Holding extends Model
use HasFactory;
use HasUuids;
protected $with = ['market_data'];
protected $fillable = [
'portfolio_id',
'symbol',
@@ -68,9 +66,6 @@ class Holding extends Model
*/
public function dividends()
{
return $this->hasMany(Dividend::class, 'symbol', 'symbol')
->select([
'dividends.symbol',
@@ -129,6 +124,16 @@ class Holding extends Model
->orderBy('date', 'DESC');
}
public function scopeWithMarketData($query)
{
$query->withAggregate('market_data', 'name')
->withAggregate('market_data', 'market_value')
->withAggregate('market_data', 'fifty_two_week_low')
->withAggregate('market_data', 'fifty_two_week_high')
->withAggregate('market_data', 'updated_at')
->join('market_data', 'holdings.symbol', 'market_data.symbol');
}
public function scopePortfolio($query, $portfolio)
{
return $query->where('portfolio_id', $portfolio);