Home > Back-end >  Is there a way to store fake attributes as an array instead of JSON?
Is there a way to store fake attributes as an array instead of JSON?

Time:07-16

I am using Laravel Backpack's fake field attribute to store clicked checkboxes into the database. But I don't want it stored as JSON but as an array for example:

Not:

{ "key" : "value }

But as:

[ "value" ]

Here is more context: https://backpackforlaravel.com/docs/5.x/crud-fields#optional-fake-field-attributes-stores-fake-attributes-as-json-in

Here is an example code snippet

protected function setupUpdateOperation()
{
    // The data for the checkboxes values
    $roles = Role::all();

    foreach ($roles as $role) {
        CRUD::field('checkbox_' . $role->id)->type('checkbox')->label($role->description)->fake(true)->store_in('checkboxes_data');
    }
}

Let me know if you need more information!

CodePudding user response:

Once you have collection you can simply iterate over it to extract values and store inside a separate array:

$roles = Role::all();
$role_values = [];

foreach($roles as $key => $value) {
    $role_values[] = $value;
}

The $role_values will now be a 1-D array containing:

['value 1','value 2', .... , 'value n']

CodePudding user response:

Found it! I just have to override the update method.

For example:

public function update($id)
{
    // Logic after update is done.
}
  • Related