Php Laravel 4-错误异常未定义变量
试图将模型绑定到表单以调用更新函数,但找不到模型Php Laravel 4-错误异常未定义变量,php,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_
{{ 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)
如果未在管线中绑定模型,则在显示/加载表单进行编辑时从控制器传递模型,例如:
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')); //<--
}
公共函数getEdit($id)
{
$upload=$this->upload->find($id);
如果(为空($upload))
{
返回Redirect::to('uploads/alluploads');
}
$this->layout->content=View::make('uploads.edit',compact('upload'));//您的绑定在哪里?您在route中绑定了模型吗?在routes中我有route::controller('uploads','UploadsController'));然后,在加载/显示表单时,您必须从控制器传递模型,如何显示表单,代码?我更新了上面的问题,我获得了所选id并将其传递到编辑视图。它的PHP错误由Laravel错误处理程序处理。实际上与Laravel无关。下面的答案很好。可能是因为if
条件,可能是$this->upload->find($id)
返回null
。在url中,我可以看到传递了正确的id。这是否意味着没有返回null?是的,那么它不是null,请删除if
条件并检查是否有效。我也删除了if条件,其他东西必须创建此空页面,我现在似乎找不到它。感谢您的帮助对。