laravel HTTP 状态代码“0”无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46362882/
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
The HTTP status code "0" is not valid
提问by Manjunath
The HTTP status code "0" is not valid.
HTTP 状态代码“0”无效。
public function filter()
{
$institute = (new Institute)->newQuery();
$institute->with(['locations','courses' => function ($query) use ($request){
$query->with('trainers');
}]);
}
$data = $institute->get();
if(count($data) > 0)
{
return response()->json($data);
}
else
{
return response()->json(404,'No Data found');
}
}
Actually, I want to display error 404 message if there is no data,
实际上,如果没有数据,我想显示错误 404 消息,
my problem is when I try to check whether data is there or not I'm getting an error called InvalidArgumentException.Can anyone help on this please.
我的问题是当我尝试检查数据是否存在时,我收到一个名为 InvalidArgumentException 的错误。请任何人帮忙解决这个问题。
回答by Sergei Krivosheenko
return response()->json(404,'No Data found');
The first argument of json should be data, then goes a status code. In this case the status code gets 0 value. Simply do it as follows:
json 的第一个参数应该是数据,然后是状态码。在这种情况下,状态代码的值为 0。只需按照以下步骤操作:
return response()->json('No Data found', 404);