laravel - 如果没有数据 Laravel 如何抛出异常?
问题描述
当我期望一些值时,如果没有数据 Laravel 如何抛出异常?
$roles = Auth::user()->roles()->get();
那么,使用:
if ($roles->count() == 0) {
throw new \Exception('No data');
}
但是如果$roles
为空怎么办?
解决方案
你可以使用这样的东西
$roles = Auth::user()->roles();
if($roles->isNotEmpty()){
$roles = $roles->get();
}
推荐阅读
- grid - TailwindCSS:如何在网格中从右侧添加元素?
- authentication - 成功登录后,我想重定向到我登录前的屏幕。getLoginRedirect() 不起作用
- c++ - 在 CPP 控制台中打印彩色 ascii 艺术
- javascript - 根据所选的下拉城市显示 google 地方自动完成
- java - 为什么我的最后一个索引不应该有一个值?
- python - 当真正的循环不起作用时在里面增加
- c# - 垂直点的坐标
- json - 在 Nifi 中转换/舍入十进制数
- arrays - C程序使用2个数组计算数字中的每个数字
- oracle - 在 WSL2 中使用 docker 构建 Go + go-oci8 时出错