Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ignore variable in update Laravel

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading