how i can make variable in Update Statement who is ignored. I tried like this but it doesn’t work.
$dl = '';
$select = DB::table('Character')->where('Name', '=', $request->char)->first();
if($select->Class == 64 OR $select->Class == 65 OR $select->Class == 66)
{
$newcom = $select->Leadership + $request->com;
$dl .= 'Leadership '.'=> ' .$newcom;
}
// Update
DB::table('Character')
->where('Name', '=', $request->char)
->update([
'Strength' => $select->Strength + $request->str,
$dl
]);
I get Invalid column name ‘0’. in error list.
>Solution :
Create an array with the data to update before the actual update. Then you can conditionally push the columns you need to update:
// The data to update, only add what should always be updated as default
$dataToUpdate = [
'Strength' => $select->Strength + $request->str,
];
// Check if we should update 'Leadership' as well
if($select->Class == 64 OR $select->Class == 65 OR $select->Class == 66)
{
// We should, add that to the array with data to update
$dataToUpdate['Leadership'] = $select->Leadership + $request->com;
}
// Update using the array we just created with the data
DB::table('Character')
->where('Name', '=', $request->char)
->update($dataToUpdate);