Attempt to read property "date" on string Route::get('/test', function () {
$stock = Stock::whereSymbol('AAPL')->first();
$dividends = $stock->dividends()
->selectRaw('ex_date as date')
->selectRaw('dividend as dividend')
->pluck('dividend', 'date')
;
// dd($dividends);
$prices = $stock->dailyClosePrices()
->selectRaw('date as date')
->selectRaw('close_price as price')
->pluck('price', 'date')
// ->values()
// ->toArray()
;
foreach($prices as $price)
{
dd($price->date);
}
return view('test', [
'prices' => $prices
]);
});
You are using pluck, so its already a date
foreach($prices as $date => $price)
{
dd($price, $date);
}
Or use get()
$prices = $stock->dailyClosePrices()
->selectRaw('date as date')
->selectRaw('close_price as price')
->get();
foreach($prices as $price)
{
dd($price->date);
}
@Sinnbeck Yes, that works but it gives me only one result when i dd($price);
@robert97 dd means dump and die ; use dump instead of you want execution to continue
@tykus Why can't i do that? I want to iterate over $prices, and then to use properties foreach price that is return.
foreach($prices as $price){
return $price ;
dump($price->date);
}
This is the result :
{"date":"2022-11-04","price":"138.38"}
@robert97 return just returns that $price, and does not allow any more iterations. Why not return $prices ?
return $prices;
@Sinnbeck Yes, that worked but gave me the collection with the 'price' property too, i only want the 'date' property. It is because of that selectRaw?
@robert97 If you dont want the prices, why select them? You just want an array of all dates ?
return $prices->pluck('date');
Let me tell you what i want to say, above this code i need to use more properties of $prices, that is why i am trying to see what it gave me back for every try.
I have a command, and here a i need to iterate over 2 collections, then use their properties for a math formula that would be "yield":
$stock = Stock::whereSymbol($this->argument('stock'))->first();
$dividends = $stock->dividends()
->selectRaw('ex_date as date')
->selectRaw('dividend as dividend')
->get();
$prices = $stock->dailyClosePrices()
->selectRaw('date as date')
->selectRaw('close_price as price')
->get();
foreach($prices as $price){
return $prices;
}
foreach($dividends as $dividend){
return $dividends;
}
$dividendYield = $stock->dividendYields();
if(!$dividendYield){
resolve(CreateDividendYieldAction::class)->create(new DividendyieldData(
stock: $stock,
date: $price->date,
yield: $dividend->dividend/$price->price*100,
price: $price->price,
yearlyDividend: $dividends->dividend
));
}
Please sign in or create an account to participate in this conversation.