Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dragonmarch's avatar

How I save data from multiple select to database column type JSON

How I code my Controller to save data from multiple select into JSON database?

My Form multiple select like:

<select class="form-control dual_select" name="selectpermission[]" id="selectpermission" multiple>
                    @foreach($permissions as $pName => $pValue)
                        <option value="{{ $pName }}"@if($pValue) selected @endif>{{ $pName }}</option>
                    @endforeach
                </select>```

My Database format in column "Permissions" should be:


```{"view-editrole":true,"view-role":true,"view-orderwaiting":true,"view-orderstatus":true,"view-company":true}```

My Controller


```public function edit(Request $request, $rid)
    {
        try{
            $permissions = $request->selectpermission; 
            foreach ($permissions as $permission) {
                echo $permission; // this is just for testing purposes
            }
                DB::table('roles')->where('id',$rid)->update(
                    .............
                );
        }catch(QueryException $e){
          
        }
        return redirect('role');
    }```


Thank you for help.
0 likes
3 replies
Cronix's avatar
Cronix
Best Answer
Level 67

the 3 tickmarks need to be on their own lines containing nothing else for the formatting to work. That code looks vaguely familiar ;)

One problem you'll have here is only the permissions that were selected in the multiselect will get sent in the request. Those are the true permissions. So, you'll also need to figure out which ones weren't sent in order to set the false permissions.

One way to do that is to have an array that has all possible permissions, and use array_diff() to determine which ones weren't sent in the request.

$selectedPermissions = $request->selectpermission;

$possiblePermissions = [
    'view-editrole',
    'view-role',
    'view-orderwaiting',
    'view-orderstatus',
    'view-company',
];

// determine the permissions that weren't sent
$unsentPermissions = array_diff($possiblePermissions, $selectedPermissions);

//create an array using $selectedPermissions and fill values to `true`
$permissions = array_fill_keys($selectedPermissions, true);

// create an array using $unsentPermissions and fill values to `false`
$unsentPermissions = array_fill_keys($unsentPermissions, false);

// merge the 2 arrays
$permissions = array_merge($permissions, $unsentPermissions);

// convert it to json
$jsonPermissions = json_encode($permissions);

// save $jsonPermissions...
DB::table('roles')->where('id',$rid)->update(['permissions' => $jsonPermissions]);

It's a lot of code to do that, and it can be shortened, but I wanted it to be self-explanatory.

1 like
dragonmarch's avatar

@Cronix

You code it worked!!!

Thank you for many help .

I'm focus on laravel functions to much and forget about PHP function.

Thank you for improve my thought.

1 like

Please or to participate in this conversation.