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

ospevack's avatar

JSON $casts as Object - Issue with conversion to string

Hi all,

i am trying to store json into a sqlite databse column, so i have the following in my eloquent model: protected $casts = [ 'json_data' => 'object'];

i have populated the database with json in the json_data field, and it converts perfectly as a object when i recall the record.

The problem arises when i try to updateOrCreate the record, i get the error:

updateOrCreate(['company_number'=>$client->company_number,'json_data'=>$data])

Object of class stdClass could not be converted to string

it doesnt make any sense to me because when i originally populated the database, the original data was entered using: updateOrCreate(['company_number'=>$client->company_number,'json_data'=>json_encode($data)])

which generated the json which the cast can read and return as an object. As i understand it the $casts type as Object should run the input through json_encode/decode as the record is recalled/saved.

does anyone have any idea why this isnt working for me? i have seen that alot of people are using the type 'array' rather than object - the rest of my code is using objects rather than arrays so i wanted to continue that convention, i also didnt see that it should make any real difference (perhaps im wrong).

thanks in advance for your help,

Oli

UPDATE

one thing of note is that the object that is being passed is generated from a json_encode call - this creates a multilayered object in PHP, it appears the error is arising at 'helpers.php on line 747:

ErrorException in helpers.php line 747: Object of class stdClass could not be converted to string in helpers.php line 747 at HandleExceptions->handleError('4096', 'Object of class stdClass could not be converted to string','/home/vagrant/Code/crmanager/vendor/laravel/framework/src/Illuminate/Support/helpers.php', '747', array('search' => '?', 'replace' => array('08715291', object(stdClass)), 'subject' => 'select * from "chdata" where ("company_number" = 08715291 and "json_data" = ?) limit 1', 'value' => object(stdClass))) at preg_replace('/?/', object(stdClass), 'select * from "chdata" where ("company_number" = 08715291 and "json_data" = ?) limit 1', '1') in helpers.php line 747

0 likes
7 replies
ospevack's avatar

Its homestead out of the box sqlite3. as i understood, by the time it gets to sqlite db it should just be a text string?

jekinney's avatar

Json encode in php converts to a json string. Basically puts " " around the json. This is fine for saving to a database. Instead of casting to and object (which is php object by the way) you may want to look at either a query scope to decode or a get attribute method to convert the string jason to a php object.

ospevack's avatar

thanks for your reply @jekinney further to your suggestion, i have removed the $casts and replaced it with: public function getJsonDataAttribute($value) { return json_decode($value); } public function setJsonDataAttribute($value) { return $this->attributes['json_data'] = json_encode($value); }

but still getting the same result.

ospevack's avatar
ospevack
OP
Best Answer
Level 1

Didnt get any further understanding why the $casts didnt work, seemed that the object got caught up in the helper function - which i assume is where json_encode is triggered.

ended up using an acccessor to return the json as an object:

public function getJsonDataAttribute($value) { return json_decode($value); }

and using json_encode as part of my updateOrCreate command:

updateOrCreate(['company_number'=>$client->company_number,'json_data'=>json_encode($data)]);

this works, its a sham i couldnt get the casts to do the same for cleanliness.

thanks for your thoughts.

JonathanAspeling's avatar

Howdy peeps! Stumbled on this thread looking for some more help why casts weren't working as expected.

This documented casts method didn't quite work for me. (https://laravel.com/docs/11.x/eloquent-mutators#attribute-casting)

I still had to use the protected variable $casts.

You can use the following from my experimentation to get it to an object for object live notation

use Illuminate\Database\Eloquent\Casts\AsArrayObject;

-------------------
    protected $casts = [
        'options' => AsArrayObject::class,
    ];


Please or to participate in this conversation.