laravel 使用 ModelNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23938061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:34:24  来源:igfitidea点击:

Using ModelNotFoundException

laravel

提问by cookie

I'm starting out in Laravel and want to discover more about using error handling especially the ModelNotFoundExceptionobject.

我从 Laravel 开始,想了解更多关于使用错误处理的信息,尤其是ModelNotFoundException对象。

<?php
 class MenuController extends BaseController {

    function f() {
          try {
                $menus = Menu::where('parent_id', '>', 100)->firstOrFail();
            } catch (ModelNotFoundException $e) {
                $message = 'Invalid parent_id.';
                return Redirect::to('error')->with('message', $message);
            }
        return $menus;
    }
  }
?>

In my model:

在我的模型中:

<?php
 use Illuminate\Database\Eloquent\ModelNotFoundException;  

 class Menu extends Eloquent {
    protected $table = 'categories'; 
}

?>

Of course for my example there are no records in 'categories' that have a parent_id > 100this is my unit test. So I'm expecting to do something with ModelNotFoundException.

当然,对于我的示例,“类别”中没有记录,parent_id > 100这是我的单元测试。所以我期待用ModelNotFoundException.

If I run http://example.co.uk/fin my browser I receive:

如果我在浏览器中运行http://example.co.uk/f,我会收到:

Illuminate \ Database \ Eloquent \ ModelNotFoundException
No query results for model [Menu].

the laravel error page - which is expected, but how do I redirect to my route 'error' with the pre-defined message? i.e.

laravel 错误页面 - 这是预期的,但是如何使用预定义的消息重定向到我的路由“错误”?IE

<?php
// error.blade.php

{{ $message }}
?>

If you could give me an example.

如果你能给我一个例子。

采纳答案by The Alpha

In Laravelby default there is an error handler declared in app/start/global.phpwhich looks something like this:

Laravel默认情况下没有宣布一个错误处理程序app/start/global.php,看起来是这样的:

App::error(function(Exception $exception, $code) {
    Log::error($exception);
});

This handler basically catches every error if there are no other specific handler were declared. To declare a specific (only for one type of error) you may use something like following in your global.phpfile:

如果没有声明其他特定的处理程序,这个处理程序基本上会捕获每个错误。要声明特定的(仅针对一种类型的错误),您可以在global.php文件中使用以下内容:

App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception) {

    // Log the error
    Log::error($exception);

    // Redirect to error route with any message
    return Redirect::to('error')->with('message', $exception->getMessage());
});

it's better to declare an error handler globally so you don't have to deal with it in every model/controller. To declare any specific error handler, remember to declare it after (bottom of it) the default error handler because error handlers propagates from most to specific to generic.

最好全局声明一个错误处理程序,这样您就不必在每个模型/控制器中处理它。要声明任何特定的错误处理程序,请记住在(其底部)默认错误处理程序之后声明它,因为错误处理程序从大多数传播到特定再到泛型。

Read more about Errors & Logging.

阅读有关错误和日志记录的更多信息。

回答by Razor

Just use namespace

只需使用命名空间

try {
    $menus = Menu::where('parent_id', '>', 100)->firstOrFail();
}catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
    $message = 'Invalid parent_id.';
    return Redirect::to('error')->with('message', $message);
}

Or refer it to an external name with an alias

或将其引用为具有别名的外部名称

use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;