Laravel Not Found Exceptions 取数据的一个小技巧

从 model 中取数据的时候, 用下面的方法, 而不是简单的 find(1), 或 first(), 这样如果刚好要查询的数据没有, 就会返回比较友好的 404 页面:

$model = AppFlight::findOrFail(1);

$model = AppFlight::where('legs', '>', 100)->firstOrFail();

Route::get('/api/flights/{id}', function ($id) {
    return AppFlight::findOrFail($id);
});
原文地址:https://www.cnblogs.com/rachelross/p/10466429.html