Michael Wright
Michael Wright

Reputation: 45

Put requests not working, Laravel 4

PUT requests were working fine for me a few days ago but now they keep returning MethodNotAllowedHttpException.

I was following this guide: http://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers.

My Create and Index work fine, however when i try to update a record it brings me that error message. Also if i change the method from PUT to POST it will start to add records.

Form

      {{ 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() }}

Controller

    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');
   }

Routes

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

Upvotes: 2

Views: 779

Answers (1)

Ronser
Ronser

Reputation: 1875

The error is that you are not passing the required url for the resource

try {{ Form::open(array('url'=>'timesheet/{id}', 'method' => 'PUT')) }} (i.e)

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

The required format is PUT/PATCH /timesheet/{id} update timesheet.update

Just use a echo $id; exit; in your controller update() to check whether root is fine...

works for me :-) and hope it helps...

Upvotes: 4

Related Questions