【问题标题】:No query results for model [modelName], laravel redirect error模型 [modelName] 没有查询结果,laravel 重定向错误
【发布时间】:2015-02-13 04:47:46
【问题描述】:

我遇到了一个问题,即我的编辑表单中的任何输入错误都返回“没有模型 [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);

}

我的意思是,我很高兴它能够捕捉输入错误,但我不想每次都将用户重定向回索引页面。我需要了解为什么它会以这种方式重定向以了解如何修复它。

【问题讨论】:

  • 您在代码中使用$validator->fails()$validator->passes() 吗?您能告诉我您的findOrFail() 代码吗?
  • findorfail() 代码来自 laravel。验证器适用于添加宠物的另一个功能。不过我没有使用失败/通过。

标签: php redirect laravel controller routes


【解决方案1】:

好的,我自己修好了,所以显然是我弄错了表格,我把它改成了:

{{ Form::open(array('action' => array('PetController@update', $pet->id), 'method' => 'put')) }}

【讨论】:

    【解决方案2】:

    所以更新会将您重定向到编辑?如果这是正确的,那么您在重定向时没有传入 id,导致它尝试查找 null 的 id,请尝试在您的编辑函数中捕获 id 是否为 null。

    【讨论】:

    • 当我点击发送时,id 正在传递。神秘的是,如果没有输入错误,我可以成功地编辑/更新更改而不会出错。但是当我输入错误时,我会被定向到编辑页面。
    猜你喜欢
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多