Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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);
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

@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)
Last updated 2 years ago.
0

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 :)

Last updated 2 years ago.
0

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);
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.