错误:{type:“ErrorException”,消息:“在Laravel 4.2中从空值创建默认对象”}

时间:2015-04-15 09:30:06

标签: php laravel laravel-4 laravel-5

当我运行下面给出的代码时,我收到以下错误

  

错误:{type:“ErrorException”,消息:“从中创建默认对象   空值“}

在此$mandate->user_id = $current_user;行。 我尝试了thisthis,但仍然显示相同的错误。感谢您的任何建议。

public function postProcess()
{
    $id = Input::get('id');
    $button_id = Input::get('button_id');
    $remarks = Input::get('remarks');
    $current_user = Sentry::getUser()->id;
    $mandate = new Mandate();
    $mandate = Mandate::find($id);

    $mandate->user_id = $current_user;
    $mandate->remarks = $remarks;

    $eventlog = new Eventlog();
    $eventlog->user_id = $current_user;
    $eventlog->mandate_id = $id;

    if($button_id == "reject"){
      $rejectreason_id = Input::get('rejectreason_id');
      $rejectreason = Input::get('rejectreason');
      $mandate->mandate_status = "Rejected";
      $mandate->rejectreason_id = $rejectreason_id;
      $eventlog->event = "Rejected: ".$rejectreason;   
    }else if($button_id == "verify") {
      $mandate->mandate_status = "Awaiting approval";
      $eventlog->event = "Verified";
    }
    else if($button_id == "approve") {
      $mandate->mandate_status = "Approved";
      $eventlog->event = "Approved";
    }

    $eventlog->save();
    $mandate->save();
    $this->generateXML($id,$rejectreason);
}

编辑1 (正如Suresh Kamrushi所建议的那样)

public function postProcess()
{
    $id = Input::get('id');
    $button_id = Input::get('button_id');
    $remarks = Input::get('remarks');
    $current_user = Sentry::getUser()->id;
    $rejectreason = "";
    $mandate = new Mandate();
    $mandate = Mandate::where('id',$id)->first();

    if(is_object($mandate)) {
      file_put_contents('log.txt', print_r("no mandate", true));
      $mandate->user_id = $current_user;
      $mandate->remarks = $remarks;
      $eventlog = new Eventlog();
      $eventlog->user_id = $current_user;
      $eventlog->mandate_id = $id;

      if($button_id == "reject"){
        $rejectreason_id = Input::get('rejectreason_id');
        $rejectreason = Input::get('rejectreason');
        $mandate->mandate_status = "Rejected";
        $mandate->rejectreason_id = $rejectreason_id;
        $eventlog->event = "Rejected: ".$rejectreason;
        //$this->generateXML($id,$rejectreason);   
      }else if($button_id == "verify") {
        $mandate->mandate_status = "Awaiting approval";
        $eventlog->event = "Verified";
      }
      else if($button_id == "approve") {
        $mandate->mandate_status = "Approved";
        $eventlog->event = "Approved";
        //$this->generateXML($id,$rejectreason);
      }

      $eventlog->save();
      $mandate->save();
    }
}

现在错误已经消失,但if语句中的代码现在无效。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

$mandate = Mandate::find($id);
if(is_object($mandate)) {
  $mandate->user_id = $current_user;
  $mandate->remarks = $remarks;
}