I have multiple columns in DB
and I need to populate the values in these fields. To achieve this, I use a foreach
loop.
$update = []; foreach ($userPermissions->getPermissionNames() as $t) { if (in_array($t, $permissions)) { $update[] = [$t => 1]; } else{ $update[] = [$t => 0]; } }
The output of this array is here:
array:2 [▼ 0 => array:1 [▼ "is_admin" => 1 ] 1 => array:1 [▼ "is_something_else" => 1 ] ]
But when it goes into updateOrInsert
it fails because the array is not structured correctly.
UserPermission::updateOrInsert(['user_id' => $user->id], $update);
How to insert these dynamic
values into table
?
How about building a simple array with keys and values?
It should produce something like,