Laravel 5.1更新表单无法正常运行

时间:2015-11-30 10:55:25

标签: forms laravel

每当我尝试更新用户时,我都会收到NotFoundHttpException in RouteCollection.php line 161:错误。我希望它可以在zoekEmail输入中填写电子邮件。它将更新数据库中的那一行。任何人都可以在以下代码中找到任何缺陷吗?

控制器功能:

public function changeMedewerker(){
        $email = Route::current()->getParameter('email');
        return User::updateMedewerker($email);
    }

模型功能:正如您在此处所见,我想检查电子邮件是否与视图中的已检查输入相同(name = zoekmail)

public static function updateMedewerker($email)
    {
        DB::table('gebruikers')
            ->where('email','=', $email)
            ->update(['email' => \Input::get('email')],
                ['username' => \Input::get('gebruikersnaam')],
                ['password' => \Input::get('wachtwoord')],
                ['voornaam' => \Input::get('voornaam')],
                ['achternaam' => \Input::get('achternaam')]);

        return redirect('/medewerkermuteren');

    }

使用的路线:

Route::put('changeMedewerker/{email}', array('as' => 'veranderMw', 'uses' =>'UserController@changeMedewerker'));

查看表单:

<form method="POST" action="/veranderMedewerker/{{Input::get('zoekmail')}}">
                   {!! csrf_field() !!}
                   <input type="hidden" name="_method" value="PUT">
                     <div class="form-group">
                       <div class="input-group">
                         <input type="text" id="zoekmail" name="zoekmail" class="form-control" placeholder="E-mail">

                       </div>
                     </div>
                     <div class="form-group">
                       <label for="email">Email address</label>
                       <input type="email" class="form-control" id="email" name="email" placeholder="E-mail">
                     </div>
                     <div class="form-group">
                       <label for="gebruikersnaam">Gebruikersnaam</label>
                       <input type="text" class="form-control" id="gebruikersnaam" name="username" placeholder="Gebruikersnaam">
                     </div>
                     <div class="form-group">
                       <label for="wachtwoord">Wachtwoord</label>
                       <input type="password" class="form-control" id="wachtwoord" name="password" placeholder="wachtwoord">
                     </div>
                       <div class="form-group">
                       <label for="voornaam">Voornaam</label>
                       <input type="text" class="form-control" id="voornaam" name="voornaam" placeholder="Voornaam">
                     </div>
                     <div class="form-group">
                       <label for="achternaam">Achternaam</label>
                       <input type="text" class="form-control" id="achternaam" name="achternaam" placeholder="Achternaam">
                     </div>
                             <div class="row">
                           <div class="col-sm-4"><button type="submit" name="veranderGebruiker" class="btn btn-success">
                             <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Verander</button>
                            </div>

                           <div class="col-sm-4"><button type="submit" class="btn btn-warning">Annuleer</button>
                            </div>
                       </div>
                   </form>

1 个答案:

答案 0 :(得分:0)

我注意到你的表单的动作指向“veranderMedewerker”,而你的路线名为“changeMedewerker”(英语等价物)。该路线还指向错误的控制器功能“changeMedewerker”而不是“updateMedewerker”。

另外,一个好的做法是使用辅助函数url('page')生成url。您也可以指向命名路线,例如:

<form method="POST" action="{{ route('veranderMw') }}">

您不需要传递'email'URL参数,因为您已经从请求中获得了该数据。我稍微重新审视了你的代码:

/**
 * Update the medewerker.
 * 
 * @param Request $request;
 */
public static function updateMedewerker(Request $request)
{
    $email = $request->input('email');

    User::whereEmail($email)->update($request->all());

    return redirect('/medewerkermuteren');
}

路线:

Route::put('updateMedewerker', ['as' => 'veranderMw', 'uses' => 'UserController@updateMedewerker'));

这应该可以解决您的问题。祝你好运。