I have multiple columns in DB
and I need to populate values in those fields. To achieve that I'm using 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 in the right structure.
UserPermission::updateOrInsert(['user_id' => $user->id], $update);
How to insert those dynamic
values into the table
?
CodePudding user response:
What about building up a simple array with the key name and value?
foreach ($userPermissions->getPermissionNames() as $t)
{
// Cast the bool to an int
$update[$t] = (int) in_array($t, $permissions);
}
It should produce something like,
[
'is_admin' => 1,
'is_something_else' => 1,
]