2014-12-01 99 views
2

PUT請求幾天前對我來說工作正常,但現在它們一直返回MethodNotAllowedHttpException。放置請求不起作用,Laravel 4

我按照這個指南:http://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

我的創建和索引工作正常,但是當我嘗試更新記錄時,它給我帶來了錯誤消息。另外,如果我將方法從PUT更改爲POST,它將開始添加記錄。

形式

 {{ Form::open(array('route' => array('timesheet.update', $timesheet->id), 'method' => 'PUT')) }} 
     {{ Form::label('companyName', 'Company Name') }} 
     {{ Form::text('companyName', $timesheet['companyName']) }} 

     {{ Form::label('placement', 'Placement') }} 
     {{ Form::text('placement', $timesheet['placement']) }} 

     {{ Form::label('startDate', 'Start Date') }} 
     {{ Form::text('startDate', $timesheet['startDate']) }} 

     {{ Form::label('endDate', 'End Date ') }} 
     {{ Form::text('endDate', $timesheet['endDate']) }} 

     {{ Form::label('weekending', 'weekending') }} 
     {{ Form::text('weekending', $timesheet['weekending']) }} 

     {{ Form::label('status', 'status') }} 
     {{ Form::text('status', $timesheet['status']) }} 




     {{ Form::submit('Edit Timesheet') }} 

    {{ Form::close() }} 

控制器

public function update($id) 
    { 
    $timesheet = Timesheet::find($id); 
    $timesheet->companyName = Input::get('companyName'); 
    $timesheet->placement = Input::get('placement'); 
    $timesheet->startDate = Input::get('startDate'); 
    $timesheet->endDate = Input::get('endDate'); 
    $timesheet->weekending = Input::get('weekending'); 
    $timesheet->status = Input::get('status'); 
    $timesheet->save(); 

    return Redirect::to('timesheet'); 
    } 

路線

Route::resource('timesheet', 'TimesheetController'); 
+0

你的功能'更新'可能不得不重新命名爲'putUpdate' – JMc 2014-12-01 16:50:52

+0

你的路線真的好嗎?你可以用'php artisan routes'來檢查你的路線 – 2014-12-01 23:17:21

+0

@JMc它是資源,不是控制器,所以不需要在函數的開頭添加'put' – 2014-12-01 23:18:27

回答

4

的錯誤是,你沒有通過所需url的資源

嘗試{{ Form::open(array('url'=>'timesheet/{id}', 'method' => 'PUT')) }}(IE)

{{ Form::open(array('url'=>'timesheet/'$timesheet->id , 'method' => 'PUT')) }} 

所需的格式是PUT/PATCH /timesheet/{id} update timesheet.update

只需用一個echo $id; exit;在你的控制器update()檢查根是否是好的...

作品對我來說:-)和希望它可以幫助...

+0

乾杯夥伴(再次! ) – 2014-12-02 12:51:05

+0

請向我解釋這一點。我有完全相同的問題。我們怎麼能不能使用指定的路線?與PUT ???? – Rafael 2015-02-03 05:19:37

+0

我也有這樣的問題。我的問題是我有一個表單內的表單。一直想弄明白。 – Rafael 2015-02-03 05:41:55