2014-02-18 62 views
0

嘗試將模型綁定到表單以調用更新函數,但未找到模型。Laravel 4 - ErrorException未定義變量

{{ 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) 
+0

你的約束力在哪裏?你是否在路線上綁定模型? –

+0

在路由中,我有Route :: controller('uploads','UploadsController'); – cch

+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

+0

這可能是因爲'if'條件,可能'$ this-> upload-> find($ id)''返回'null'。 –

+0

在url中,我可以看到正確的id被傳遞。這是否意味着沒有返回null? – cch

+0

是的,它不是空的,刪除'if'條件並檢查是否工作。 –

相關問題