首页 > 解决方案 > 在 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'));
        }
    }

标签: phplaravel

解决方案


如果用户未通过身份验证,那么您将收到意外错误。所以首先检查authasAuth::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'));
            }
        }

推荐阅读