2017-06-08 104 views
0

我掙扎過雄辯的模型Laravel 5.4Laravel 5.4 - 更新現有記錄

我對創建工作完全正常的記錄更新現有的記錄,我把它加以改造,讓它嘗試更新記錄:

public function commitEdit ($char_edit_id) 
    { 


    $edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->first(); 


    $edited_character->campaign_id = 1; 

    $edited_character->character_name = request('characterName'); 

    $edited_character->Race = request('race'); 

    $edited_character->Sub_Race = request('subRaceField'); 

    $edited_character->Class = request('class'); 

    $edited_character->Level = request('level'); 

    $edited_character->Strength = request('strength'); 

    $edited_character->Dexterity = request('dexterity'); 

    $edited_character->Constitution = request('constitution'); 

    $edited_character->Intelligence = request('intelligence'); 

    $edited_character->Wisdom = request('wisdom'); 

    $edited_character->Charisma = request('charisma'); 

    $levelVar = request('level'); 

    if ($levelVar >= 4) { 
    $edited_character->Proficiency = 2; 
    } else if ($levelVar >= 8) { 
    $edited_character->Proficiency = 3; 
    } 


    $edited_character->Trained_Skills = request('skillsField'); 

    $edited_character->Languages = request('languagesField'); 


    $edited_character->Hit_Die = 1; 

    $edited_character->max_HP = request('max-hp'); 

    $edited_character->Alignment = request('alignment'); 

    $edited_character->Armor_Class = request('armor-class'); 

    $edited_character->Initiative = request('initiative'); 

    $edited_character->Speed = request('speed'); 

    $edited_character->Background = request('background'); 



    $edited_character->update(); 

    return redirect('./characters'); 

這給了這個錯誤: 調用未定義的方法stdClass的::更新()

我已經使用save()嘗試,但我得到保存相同的錯誤(),而不是更新()

在此先感謝C:

回答

0

Documentation

If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single StdClass object:

$edited_characterstdClass,沒有伶牙俐齒的模型。

你可以試試這個代碼:

public function commitEdit ($char_edit_id) 
{ 
    $edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->update([ 
     'campaign_id' => 1, 
     'character_name' => request('characterName'), 
     'Race' => request('race'), 
     //others property 
    ]); 
} 

或者創建Characters模型,這將是從Illuminate\Database\Eloquent\Model延伸,並使用save方法:

public function commitEdit ($char_edit_id) 
{ 
    $edited_character = Characters::where('char_id', $char_edit_id)-first(); 
    //your code with properties 
    $edited_character->save(); 
} 
0

你可以試試這個方法:

public function commitEdit ($char_edit_id) 
{ 
    $edited_character = Characters::find($char_edit_id); 
    $edited_character->character_name = request('characterName'); 

    $edited_character->Race = request('race'); 

    $edited_character->Sub_Race = request('subRaceField'); 

    $edited_character->Class = request('class'); 

    $edited_character->Level = request('level'); 

    $edited_character->Strength = request('strength'); 

    $edited_character->Dexterity = request('dexterity'); 

    $edited_character->Constitution = request('constitution'); 

    $edited_character->Intelligence = request('intelligence'); 

    $edited_character->Wisdom = request('wisdom'); 

    $edited_character->Charisma = request('charisma'); 

    $levelVar = request('level'); 

    if ($levelVar >= 4) { 
    $edited_character->Proficiency = 2; 
    } else if ($levelVar >= 8) { 
    $edited_character->Proficiency = 3; 
    } 


    $edited_character->Trained_Skills = request('skillsField'); 

    $edited_character->Languages = request('languagesField'); 


    $edited_character->Hit_Die = 1; 

    $edited_character->max_HP = request('max-hp'); 

    $edited_character->Alignment = request('alignment'); 

    $edited_character->Armor_Class = request('armor-class'); 

    $edited_character->Initiative = request('initiative'); 

    $edited_character->Speed = request('speed'); 

    $edited_character->Background = request('background'); 



    if($edited_character->save()){ 
     return redirect('./characters'); 
    }else{ 
     // show error message 
     } 
}