Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robert97's avatar

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
]);

});

0 likes
10 replies
Sinnbeck's avatar

You are using pluck, so its already a date

foreach($prices as $date => $price)
{
    dd($price, $date);
}
1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Or use get()

    $prices = $stock->dailyClosePrices()
        ->selectRaw('date as date')
        ->selectRaw('close_price as price')
        ->get();

foreach($prices as $price)
{
    dd($price->date);
}
1 like
tykus's avatar

@robert97 dd means dump and die; use dump instead of you want execution to continue

1 like
robert97's avatar

@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"}

Sinnbeck's avatar

@robert97 return just returns that $price, and does not allow any more iterations. Why not return $prices ?

return $prices;
1 like
robert97's avatar

@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?

Sinnbeck's avatar

@robert97 If you dont want the prices, why select them? You just want an array of all dates ?

return $prices->pluck('date');
robert97's avatar

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.

robert97's avatar

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 or to participate in this conversation.