2017-07-23 35 views
0

当我试图用我的表格中的value=""字段更新我的表时,查询失败并返回false,但我无法收到错误,告诉我什么是错误的任何方式。更新查询返回false使用表格中的预设值

$input = Input::get("textfield"); 
$option = Input::get("optionfield"); 

if($option == 0) 
{ 
    $option = null; 
} 

if(empty($input)) 
{ 
    $input = null; 
} 

$update = MyTable::where("id", "=", 1)->update(["input" => $input, "option" => $option]); 

如果我通过在输入字段中输入内容来更改值,那么它会更新。

怎么回事?

+0

如果值为空,为什么需要更新只是跳过更新并在输入值为空时返回 –

+0

旧值可能不为空,如果该字段为空,则应将其设置为空 –

+0

似乎您遇到问题'null'值。你尝试过'$ option ='';'和'$ nput ='';'?这两个字段都是字符串吗? –

回答

0

基本上,当我们更新现有数据时,我们会比较新条目和旧条目。在你的情况下,你没有比较完成。

所以,让我们做到这一点。我通常做这在我自己的情况:

  • 获取现有的数据第一

    $row = MyTable::where("id", 1)->first(); 
    
  • 然后,比较数据

    $old_data = $row->toArray(); // convert to array first 
    $new_data = Input::all(); // assuming all inputs are allowed for updates 
    
    // now, lets compare values with additional index check 
    $changes = array_diff_assoc($new_data, $old_data); 
    
    // update row if has modifications 
    if(!empty($changes)) { 
        $row->update($changes); 
    } 
    

希望这将有助于解决您的问题。

相关问题