【问题标题】:Updating a column giving error in laravel 4更新在 laravel 4 中给出错误的列
【发布时间】:2015-06-10 06:55:17
【问题描述】:

我正在尝试更新 laravel 中名为“is_shared”的列,但它给出了错误。

我试过了。

public static function setShared($key, $status = true){
    $input = ScorecardInputMeta::where("key", $key)->first();
    if($input){
       $input->is_shared = $status;
       $input->save;
    }
}//end function

错误: {"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"调用 到成员函数 getResults() on boolean","file":"D:\Projects\hbi_private\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php","line":2070}}

【问题讨论】:

  • 你忘记了最重要的部分......实际的错误!
  • @user3621494 我现在已经写了错误信息
  • 在假设它是一个对象之前,您应该检查 $status 不是 null 或者不是您所期望的(当您的查询 IIRC 没有结果时)。另外,ScorecardInputMeta 是一个合适的雄辩模型吗?
  • 检查$input 以获得null 或使用firstOrFail
  • 请使用 dd($input) 检查 $input 中的返回值。

标签: php laravel laravel-4 eloquent query-builder


【解决方案1】:

假设ScorecardInputMetaEloquent Model,试试:

public static function setShared($key, $status = true)
{
    // ->where(column, operator, value)
    $input = ScorecardInputMeta::where('key', '=', $key)->first();

    // check if input is not null or an object
    if ($input) {
        $input->is_shared = $status;
        $input->save(); // changed from $input->save
    }
}

【讨论】:

  • $input->save 给出错误,其他一切正常
  • $input->save();行给出错误,其他一切正常
  • 你能展示整个ScorecardInputMeta类吗?
猜你喜欢
  • 2016-07-22
  • 2013-06-02
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 2013-05-28
  • 2019-05-06
  • 2013-02-28
  • 2020-12-04
相关资源
最近更新 更多