我遇到了一个问题,即编辑表单中的任何输入错误都返回"模型 [modelName] 没有查询结果",而不是像它应该的那样生成验证器消息。这是我的代码:
宠物控制器.php
public function update($id)
{
//omited the validator messages cause it's too long, but it works cause I have an add function that uses the same thing.
try {
$pet = Pet::findOrFail(Input::get('id'));
}
catch(exception $e) {
return Redirect::to('/pet')->with('flash_message', 'Error Editing Pet.');
}
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$breed = filter_var($_POST["breed"], FILTER_SANITIZE_STRING);
$birthday = filter_var($_POST["birthday"], FILTER_SANITIZE_STRING);
$pet->name = $name;
$pet->breed = $breed;
$pet->birthday = Pet::saveDateFmt($birthday);
$pet->save();
return Redirect::to('/pet')->with('flash_message','Your changes have been saved.');
}
因此,任何带有名称、品种或日期的输入错误都会将我重定向到/pet,并显示来自此函数的错误消息:
public function edit($id){
try {
$pet = Pet::findOrFail($id);
}
catch(exception $e) {
return Redirect::to('/pet')
->with('flash_message', $e->getMessage());
//this is where I get the error msg
}
return View::make('edit_pet', ['vet_list' => Vet::lists('name','id')])->with('pet', $pet);
}
我的意思是,我很高兴它捕获了输入错误,但我不想每次都将用户重定向回索引页面。我需要了解为什么它以这种方式重定向以了解如何修复它。
所以更新会将您重定向到编辑?如果这是正确的,那么当您重定向时,您不会传入 id,导致它尝试查找 id 为 null,请尝试在您的编辑函数中捕获 id 是否为 null。
好的,我只是自己修复了它,所以显然我弄错了形式,我将其更改为:
{{ Form::open(array('action' => array('PetController@update', $pet->id), 'method' => 'put')) }}