If you use string as a column you need to serialize the array to a string representation.
In case of plain php Array you could use json_encode($array)
before inserting.
In a reverse operation (reading from database) you would do
$readArray = json_decode($model->field);
You either need to serialize it or json encode it beforehand. Keep in mind, though, that you more or less lose the ability to search that data. Depending on your circumstances it might be better to add another table.
@marekmurawski @shabushabu : thanks
but i'm getting the return type as array but unable to parse it
array(1) { [0]=> object(stdClass)#170 (3) { ["year"]=> string(1) "9" ["dept"]=> string(2) "AZ" ["section"]=> string(1) "BC" } }
when I use $array_var[0]->year, I'm getting
Trying to get property of non-object (View: /app/views/staff/list.blade.php)
Instead of JSON, saved the array as serialized object
Then while displaying,
$std = unserialize($user->children);
echo $std[0]['year'];
This way, it worked.
thanks again :)
How I do it:
override the 2 accessor methods:
public function get<Column-Name>Attribute {
return json_decode($this->attributes['Column-Name']);
}
public function set[Column-Name]Attribute(Array $val) {
$this->attributes['Column-Name'] = json_encode($val);
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community