Laravel 5.4 - 更新现有记录

时间:2017-06-08 20:02:56

标签: php laravel laravel-5.4

我正努力通过Laravel 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 :: update()

我尝试过使用save()但是我用save()而不是update()

得到了同样的错误

先谢谢c:

2 个答案:

答案 0 :(得分:0)

Documentation

  

如果您只需要从数据库表中检索单行,则可以使用第一种方法。此方法将返回单个StdClass对象:

$edited_characterstdClass,没有Eloquent模型。

您可以尝试以下代码:

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();
}

答案 1 :(得分: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
       }
}