Laravel 4 - ErrorException未定义的变量

时间:2014-02-18 23:49:34

标签: php

尝试将模型绑定到表单以调用更新函数,但找不到模型。

{{ Form::model($upload, array('url' => array('uploads/update', $upload->id), 'files' => true, 'method' => 'PATCH')) }}

控制器以获取编辑视图

public function getEdit($id)
{
 $upload = $this->upload->find($id);

if (is_null($upload))
{
  return Redirect::to('uploads/alluploads');
}

 $this->layout->content = View::make('uploads.edit', compact('uploads'));
}

控制器进行更新

public function patchUpdate($id)
{
  $input = array_except(Input::all(), '_method');

  $v = Validator::make($input, Upload::$rules);

  if ($v->passes())
  {
    $upload = $this->upload->find($id);
    $upload->update($input);

    return Redirect::to('uploads/show', $id);
  }

  return Redirect::to('uploads/edit', $id)
   ->withInput()
   ->withErrors($v)
}

错误我

ErrorException
Undefined variable: upload (View: /www/authtest/app/views/uploads/edit.blade.php)

1 个答案:

答案 0 :(得分:1)

如果您没有在路线中绑定模型,则在显示/加载表单进行编辑时将其从控制器传递,例如:

public function getEdit($id)
{
    $upload = $this->upload->find($id);
    if (is_null($upload))
    {
        return Redirect::to('uploads/alluploads');
    }
    $this->layout->content = View::make('uploads.edit', compact('upload')); //<--
}

更新:您应使用compact('upload')而不是uploads,因为该变量为$upload