Saving model with json column will save an escaped json if cast. Am I missing something here? Is this intentional?
If not cast,
[{"code": "a", "name": "a"}, {"code": "b", "name": "b"}, {"code": "c", "name": "c"}]
If cast,
"[{\"name\":\"a\",\"code\":\"a\"},{\"name\":\"b\",\"code\":\"b\"},{\"name\":\"c\",\"code\":\"c\"}]"
Add to model
protected $casts = ['tags' => 'array'];
Then save the model
Looks like something in your code is already serializing the attribute so when Eloquent serializes it again it appears like this, check your code as the current settings work just fine.
Thanks, I tried something just now and now I get what the cast attribute is really doing.
@adifaidz How do you fix it? still use model cast or not?
@adifaidz When you store to DB, You should store in array format not json_encode()
.
@ThunderBirdsX3 If I do like that. I will get mysql error "Array to string conversion", Because I use model cast. It save array as json automically.
@ALTELMA Look like your database version not support. Or your column not JSON type.
My database support column json type. I can store directly, but cannot save from Laravel.
Bad Example:
Insert into users ('name', 'meta') VALUES ('ALTELMA', {"facebook":"","google":"","linked":""})
Should be:
Insert into users ('name', 'meta') VALUES ('ALTELMA', '{"facebook":"","google":"","linked":""}')
I don't know why laravel save value without quote.
Show your Model and code.
Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'meta' => 'array',
];
Save
$user = new User;
foreach($inputs as $key => $value) {
$user->{$key} = $value;
}
$user->save();
@ALTELMA User::create(['name' => $inputs->name, 'meta' => $inputs->meta]);
I'm not sure, What is your $inputs
it's $request
?
Yes, it's $request, but why I cannot use save()?
@ALTELMA What is your data in meta look like? dd($request->meta);
Data store in array format like this.
["facebook":"123", "twitter":"456"]
Actually that's example data. Main point is when save data should be "{}" not {}.
I think I have problem only save() function.
Thanks Mohamed (@themsaid)
@adifaidz When you store to DB, You should store in array format not
json_encode()
.
This comment alone has saved my day. I had the same issue - casting JSON column and spent ~2 hours last night trying to fix it and couldn't. Following the advice helped me resolve it.
Thank you!
Most helpful comment
Looks like something in your code is already serializing the attribute so when Eloquent serializes it again it appears like this, check your code as the current settings work just fine.