编辑用户时出现Laravel 4 MethodNotAllowedHttpException


Laravel 4 MethodNotAllowedHttpException when editing user

我不确定为什么这种情况会一直发生,我尝试了几种不同的解决方案,但似乎都不起作用:/。我在我的laravel应用程序中有一个编辑视图,我正在尝试更新内容,但我一直收到MethodNotAllowedHttpException错误。

编辑视图

@extends('layouts.master')
@section('content')
<div class="container profile-wrapper" style="position: relative;top:170px;">
        <div class="col-lg-8 profile-content">
            <div class="panel panel-default">
                <div class="panel-heading">{{ucwords($user->firstname)}}, Update Your Profile Below</div>
                @if($errors->any())
                <div class="alert alert-error">
                    <a class="close" data-dismiss="alert" href="#" aria-hidden="true">&times;</a>
                    {{ implode('', $errors->all('<li class="error">:message</li>')) }}
                </div>
                @endif
                <div class="panel-body">
                    {{ Form::open(array('method' => 'PUT','url'=>'user/profile/update', 'class'=>'form-inline edit-form')) }}

                    <fieldset>
                        <legend>Personal Information</legend>
                            <div class="form-group">
                                <label for="firstname">First Name</label>
                                <input type="text" value="{{$user->firstname}}" class="form-control input-sm" id="firstname" placeholder="Enter Your First Name">
                            </div>
                            <div class="form-group">
                                <label for="lastname">Last Name</label>
                                <input type="text" value="{{$user->lastname}}" class="form-control input-sm" id="lastname" placeholder="Enter Your Last Name">
                            </div>
                        <div class="form-group">
                            <label for="zipcode">Zip Code</label>
                            <input type="text" value="{{$user->zipcode}}" class="form-control input-sm" id="zipcode" placeholder="Enter Your 5 digit Zip Code">
                        </div>

                    </fieldset>

                    <fieldset>
                        <legend>Professional Information</legend>

                        <div class="form-group">
                            <label for="experience">Years Experience</label>
                            <input type="text" value="{{$user->temp_experience}}" class="form-control input-sm" id="experience" placeholder="Enter Years of Experience">
                        </div>

                        <div class="form-group">
                            <label for="hourlyrate">Hourly Rate</label>
                            <input type="text" value="{{$user->temp_hourly_rate}}" class="form-control input-sm" id="hourlyrate" placeholder="Enter Your Hourly Rate">
                        </div>
                        <div class="form-group">
                            <label for="zipcode">Willingness to Travel</label>
                            <select name="temp_travel" class="form-control input-sm">
                                <option value="{{$user->temp_travel}}">{{$user->temp_travel}}</option>
                                <option value="1-15">1-15</option>
                                <option value="15-25">15-25</option>
                                <option value="25-35">25-35</option>
                                <option value="35-45">35-45</option>
                                <option value="35-50">35-50</option>
                            </select>
                        </div><!-- end .form-group -->
                        <!-- conditional statement for different temp types -->
                        <div class="form-group mtop-20">
                            <label for="temp_software_experience">Software Experience:</label><br>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Practiceworks"> Practiceworks
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Softdent"> Softdent
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Ortho trac"> Ortho trac
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Dentrix"> Dentrix
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Easy Dental"> Easy Dental
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Practiceworks"> Eaglesoft
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" id="temp_software_experience" value="Other"> Other
                            </label>
                        </div><!-- end .form-group -->

                    </fieldset>

                    <div class="form-group">
                        {{Form::hidden('id', $user->id)}}
                        {{ Form::submit('Update', array('class'=>'btn btn-primary form-control'))}}
                    </div>
                        {{ Form::close() }}
                    </div><!-- end .panel-body -->
                </div><!-- end .panel/default -->
            </div><!-- end .profile-content -->
            <div class="col-lg-4">
                image
            </div>
        </div><!-- end .container/profile-wrapper -->
@stop

路线路由::put('user/profile/update/',array('uses'=>'UserController@putUpdate'));

控制器

public function putUpdate()
    {
        $id = Input::get('id');
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'firstname' => 'required|min:3|alpha',
            'lastname' => 'required|min:3|alpha',
            'zipcode' => 'required|min:5|numeric'
        );
        $validator = Validator::make(Input::all(), $rules);
        // process the login
        if ($validator->fails()) {
            return Redirect::to('user/profile/' . $id . '/edit')
                ->withErrors($validator)
                ->withInput();
        } else {
            // store
           User::update($id, array(
            'firstname'       => Input::get('firstname'),
            'lastname'      => Input::get('lastname'),
            'zipcode' => Input::get('zipcode')
        ));
            // redirect
            return Redirect::to_route('show', $id)->with('message', 'User Updated!');
        }
    }

您必须通过表单传递ID。

更改以下行

{{ Form::open(array('method' => 'PUT','url'=>'user/profile/update', 'class'=>'form-inline edit-form')) }}

{{ Form::open(array('method' => 'PUT','url'=> array('user/profile/update', $user->id), 'class'=>'form-inline edit-form')) }}

在控制器中:

public function putUpdate($user_id)

如果您使用put作为http方法,Laravel将需要一个带有表单的ID。请使用form::model()进行编辑。如果您使用Form::model(),Laravel将自动填写表单,您不必执行"{{$user->temp_hourly_rate}}"