why don't you simply convert it into a json string to save and back into an array when you get it?
Yo ucan do so by either changing the infraction_data and encouragement_data fields from string to json type in your migration or by simply adding 2 mutator methods to the Inspection Model:
public function getInfractionDataAttribute()
{
return isset($this->attributes['infraction_data'])
? json_decode($this->attributes['infraction_data'], true)
: [];
}
public function setInfractionDataAttribute($infractionData)
{
$this->attributes['infraction_data'] = json_encode($infractionData);
}
// and do the same for encouragement_data
public function getEncouragementDataAttribute()
{
return isset($this->attributes['encouragement_data'])
? json_decode($this->attributes['encouragement_data'], true)
: [];
}
public function setEncouragementDataAttribute($encouragementData)
{
$this->attributes['encouragement_data'] = json_encode($encouragementData);
}
From now on you can simply do this in your controller:
// creating a new row
$inspection = Inspection::create([
'infraction_data' => $request->infractions,
'encouragement_data' => $request->encouragement,
'user_id' => auth()->id(),
'requisition_id' => 1
]);
// getting the options from some row in the DB
$inspection = Inspection::find(1);
dd( $inspection->encouragement_data ); // shows the whole array