0

I have a form with a series of checkboxes. The name of the checkboxes is name="groups[]".

<input type="checkbox" name="groups[]" value="value-1">
<input type="checkbox" name="groups[]" value="value-2">
<input type="checkbox" name="groups[]" value="value-3">
<input type="checkbox" name="groups[]" value="value-4">

I want to save the values submitted to a column named groups in a MySQL database. My Laravel update code is as follows:

$record->groups = $attributes['groups'];
$record->save();

I have also tried:

$record->groups = implode(',', $attributes['groups']);
$record->save();

Just in case it helps, the field details in the migration file is:

$table->set('groups', ['value-1','value-2','value-3','value-4'])->nullable();

The error receiving is:

SQLSTATE[01000]: Warning: 1265 Data truncated for column 'groups' at row 1 (SQL: update `table` set `groups` = ["value-1","value-2"] where `id` = 1)

Is my update code incorrect for Laravel and Set columns?

I tried Googling a lot, but because the term set is used in regular update queries, I'm not getting helpful results.

Edit: Added Table Structure

Column Type
id bigint(20)
groups set('value-1','value2','value-3','value-4')

Edit: Results of my attributes:

array: 1 [▼
  "groups" => array:2 [▼
    0 => "value-1"
    1 => "value-2"
  ]
]
'''
Adam
  • 125
  • 1
  • 12

1 Answers1

0

It ends up that the correct method to update a set type column is to implode the array into a comma separated string. I thought I tried this earlier, but it's working now, not actually sure why it wasn't before and is now. Here is the working update:

$proposal->groups = implode(',', $attributes['groups']);
$proposal->save();
Adam
  • 125
  • 1
  • 12