php - 在 null 上调用成员函数 get() - LARAVEL
问题描述
再次寻求您的帮助。只是想知道如果该列为 NULL,是否有任何想法返回一些东西。请看下面的代码:谢谢!
应用控制器
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
解决方案
如果用户未通过身份验证,那么您将收到意外错误。所以首先检查auth
asAuth::check()
尝试这个 :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
推荐阅读
- android - 如何更新 alertDialog 中的按钮颜色变量?
- cuda - CUDA 根据字长合并内存访问速度
- python - ARIMA 模型的最佳信息标准?
- php - PHP 无法上传大视频到 SQL 数据库
- mysql - 创建包含来自两个表的数据的数组
- google-app-maker - 应用制作工具中的数据事件/服务器端事件
- php - 将变量传递到包含的文件中
- assembly - 如何在 AVR 的汇编中将 SRAM 地址从 60 增加到 61 到 62
- c# - 实例化类给出该名称不存在
- android - 如何在 android 的 MPV 中使用“onClickListener”?