我如何在被忽略的更新陳述句中創建變數。我試過這樣,但它不起作用。
$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
]);
我得到無效的列名'0'。在錯誤串列中。
uj5u.com熱心網友回復:
這應該可以解決您的問題:
$select = DB::table('Character')->where('Name', '=', $request->char)->first();
if($select->Class == 64 OR $select->Class == 65 OR $select->Class == 66)
{
$select->Leadership = $select->Leadership $request->com;
}
$select->Strength = $select->Strength $request->str;
$select->update();
由于您已經擁有該$select物件,您只需Leadership有條件地更新屬性并推送更新。
uj5u.com熱心網友回復:
在實際更新之前創建一個包含要更新的資料的陣列。然后可以有條件地推送需要更新的列:
// 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);
uj5u.com熱心網友回復:
嘗試使用
$dl .= 'Leadership '.'=> ' .$newcom;
創建一個陣列元素是行不通的。它只是創建一個試圖分配給列的字串0(分配給它的默認陣列索引)。
相反,您應該構建一個適當的關聯資料陣列并將其用于更新,因此在創建陣列時分配主要值。然后添加任何可選值(例如領導力)。就像是...
$data = ['Strength' => $select->Strength $request->str,];
if($select->Class == 64 OR $select->Class == 65 OR $select->Class == 66)
{
$newcom = $select->Leadership $request->com;
$data['Leadership'] = $newcom;
}
// Update
DB::table('Character')
->where('Name', '=', $request->char)
->update($data);
將最終陣列傳遞給您的更新陳述句。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514633.html
標籤:php拉拉维尔
