Reputation: 406
So, i'm trying to store data in database as JSON, i've seen the built-in notification table doing that.
I created a field of type "text" and i'm storing this (example)
{"filenames":["D0BmpFOdyUp4YyA8tqpL60VxO9kXsfciP9eLhIxd.jpeg","nAAs1xeErQSxPGxPvP68bCMn4a0E3y4EcgwfCwJl.png"],"title":"asdasdasa","content":"adasdasa"}
It returns me string as expected, how do i make it understand as an array?
Do i have to add something in the model or...?
Thanks.
Upvotes: 1
Views: 6173
Reputation: 1507
See attribute Casting in Laravel's documentation. In your Eloquent model class, add a $casts
property, for example:
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'your_column_name' => 'array'
];
From Laravel Docs:
The
$casts
property on your model provides a convenient method of converting attributes to common data types. The$casts
property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to.
Upvotes: 5