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.
3
Answers
Trying to use
to create an array element won’t work. It just creates a string which it’s trying to assign to column
0
(the default array index assigned to it).Instead, you should build a proper associative array of data and use that for the update, so assign the main values when you create the array. Then add any optional values as you go along (such as Leadership). Something like…
Passing the final array to your update statement.
Create an array with the data to update before the actual update. Then you can conditionally push the columns you need to update:
This should fix your issue:
since you already have the
$select
object, you just have to updateLeadership
properties conditionally and push the update.