Have you tried casting to json instead?
protected $casts = [
'extra' => 'json'
];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Only a few moments ago, my code worked as intended, then I called php artisan migrate:fresh to clear out my database and things stopped working.
All I want to do is to retrieve properties from my JSON column, and then update it. But whenever i try to retrieve the value, it says
ErrorException Illegal string offset 'starred'
This is my code:
inventory.php
protected $guarded = [];
protected $casts = [
'extra' => 'array'
];
InventoryController.php
public function visible($code) {
$inventory = Inventory::where('code', $code)->firstOrFail();
if ($inventory->extra['starred'] === true) {
$inventory->update([
'extra->starred' => false
]);
} else {
$inventory->update([
'extra->starred' => true
]);
}
return $inventory;
}
Any clue to whatever is going on here would be much appreciated! It was working as intended not too long ago, but now - it's not. This is called through an API.
Please or to participate in this conversation.