Does this require a nested loop? Say there are 10 items in $dailyClosePrices and 5 items in $dividends; then should you have 50 DividendyieldData instances after looping?
Nov 4, 2022
4
Level 1
Iterate over two collections
Hello, i am trying to get the result from a command, and in this result i need to pass properties from two different collection from my DB. what can i pass in that foreach loop to get the results?
$stock = Stock::whereSymbol($this->argument('stock'))->first();
$dailyClosePrices = DailyClosePrice::where('stock_id', $stock->id)
->selectRaw('date(date) as date')
->selectRaw('close_price as price')
->orderBy('date')
->get();
$dividends = Dividend::where('stock_id', $stock->id)
->selectRaw('ex_date as date')
->selectRaw('dividend as dividend')
->orderBy('date')
->get();
$dividendYield = DividendYield::where('stock_id', $stock->id)->first();
if (!$dividendYield) {
foreach( ... ) {
resolve(CreateDividendYieldAction::class)->create(new DividendyieldData(
stock: $stock,
date: $dailyClosePrices->date,
yield: $dividends->divident / $dailyClosePrices->price * 100,
price: $dailyClosePrices->price,
yearlyDividend: $dividends->dividend
));
}
}
Please or to participate in this conversation.