phplaravelvue.jsinertiajslaravel-jetstream

Inertia handle null in share middleware


    public function share(Request $request)

    {

        return array_merge(parent::share($request), [

            'darkMode' => !!$request->user()->dark_mode,

        ]);

    }

So the code above is saying "use darkMode" once a user is logged in. The issue is, if a user is NOT logged in it comes back as NULL. How do I set a default value if it comes back as null (the default would be "false")


Solution

  • What about:

    public function share(Request $request)
    {
       return array_merge(parent::share($request), [
          'darkMode' => Auth::check() ? Auth::user()->dark_mode : false,
       ]);
    }