[英]Return value if exists in laravel
I'm trying to get data with firstOrFail() to getting data with eloquent laravel. 我正在尝试使用firstOrFail()获取数据,以雄辩的laravel获取数据。 but facing an issue, when i'm trying to fetch the data is not exist then it will return me model exception.
但是面临一个问题,当我尝试获取数据不存在时,它将返回模型异常。 How can i make it into return data if the data is exist while return null or "" if the value or field doesn't exist.
如果数据存在,如何将其转换为返回数据;如果值或字段不存在,则如何返回null或“”。 I tried to add the following but it will lead the page into total blank.
我尝试添加以下内容,但它将导致页面空白。
(global.php) (global.php)
use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
// return Response::make('Not Found', 404);
return "";
});
Instead of using firstOrFail
, just use first
: 除了使用
firstOrFail
,还可以使用first
:
$model = Model::first();
If no model was found, $model
will be null
. 如果未找到任何模型,则
$model
将为null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.