2014-12-01 42 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