处理在 Laravel 中找不到的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38264997/
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
Dealing with data not found in Laravel
提问by Connor Bishop
Laravel has a built-in method called findOrfail()
described here:
Laravel 有一个内置方法,称为findOrfail()
描述在这里:
Not Found Exceptions
Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The findOrFail and firstOrFail methods will retrieve the first result of the query. However, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown:
$model = App\Flight::findOrFail(1);
$model = App\Flight::where('legs', '>', 100)->firstOrFail();
未找到异常
有时,如果找不到模型,您可能希望抛出异常。这在路由或控制器中特别有用。findOrFail 和 firstOrFail 方法将检索查询的第一个结果。但是,如果没有找到结果,则会抛出 Illuminate\Database\Eloquent\ModelNotFoundException:
$model = App\Flight::findOrFail(1);
$model = App\Flight::where('legs', '>', 100)->firstOrFail();
But what if you want to write your own code which deals with no result found. So in my controller I want something like:
但是,如果您想编写自己的代码来处理未找到的结果怎么办。所以在我的控制器中,我想要这样的东西:
public function show($id)
{
$data = JobCardHead::where('JobCardNum', $id)->first();
If no data found then
some code
else
some code
回答by kix
What does Eloquent return in case no model is found for the methods that do not throw an exception? I'm guessing it's either null
or false
. So, you could check the return value of the first()
call:
如果找不到不抛出异常的方法的模型,Eloquent 会返回什么?我猜它要么null
或false
。因此,您可以检查first()
调用的返回值:
$data = JobCardHead::where('JobCardNum', $id)->first();
if ($data) {
// found it
} else {
// not found
}
Or, knowing that firstOrFail()
throws an exception, you could wrap the call in a try/catch block:
或者,知道会firstOrFail()
引发异常,您可以将调用包装在 try/catch 块中:
use Illuminate\Database\Eloquent\ModelNotFoundException;
//..
try {
$data = JobCardHead::where('JobCardNum', $id)->first();
} catch (ModelNotFoundException $e) {
// Data not found. Here, you should make sure that the absence of $data won't break anything
}
// Here $data is an instance of the model you wanted
The exact approach you should pick actually depends on your usecase a lot.
您应该选择的确切方法实际上很大程度上取决于您的用例。
UPD.By the way, if $id
here is your primary key here, you should simply use find($id)
or findOrFail($id)
, as described here
更新。顺便说一句,如果$id
这里是您的主键,您应该简单地使用find($id)
or findOrFail($id)
,如此处所述
回答by Steve Bauman
Laravel returns null
on first()
calls to your model when no records are returned.
当没有记录返回时,Laravel 会null
在first()
调用您的模型时返回。
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Query/Builder.php#L1548
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Query/Builder.php#L1548
So for example you could do this as an even cleaner solution:
因此,例如,您可以将其作为更清洁的解决方案来执行:
public function show($id)
{
if ($job = JobCardHead::where('JobCardNum', $id)->first()) {
// Record was found!
return view('job.show', compact('job'));
}
return view('404');
}