如何在程序中使用 Laravel 7 中的错误?

How to use error in Laravel 7 in program?

我想在我的代码中使用这个错误,例如

if(Not Found In Database) then
show alert"Data Not Found In Database"

如何将该错误包含到我的变量中?

*注意:我使用的是Laravel-7

首先检查你有没有记录:

$data = model::all();
if($data == NULL) {
  echo "<script>alert('data not found')</script>"
}

你可以试试

throw new \Exception("error");

编辑,增加使用curl

检查文件是否存在的功能
function checkUrlExistFile($url): bool {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_NOBODY, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_exec($ch);
  $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  if($info == '200') return true;
  return false;
}

和例子

if (!checkUrlExistFile($url)) {
  throw new \Exception("error");
}