PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Error Modifying welcome.blade.php

Solved
ookma-kyi

Aug 15th, 2023 01:39 PM

I am learning Laravel and have run into an issue. I tried to modify the welcome.blade.php template and replaced it with my content. When I tried to view my changes I got an error page. This is the contents of the welcome.blade.php file:

@extends('layouts.app')

<h1>Welcome</h1>

<p>Sample Text</p>

Here is a link to the flare error log.

thinkverse

Aug 15th, 2023 01:47 PM

Auth::user() returns the currently logged in user, if a user is not logged in it will return null, and you cannot access properties on a null object.

You can use the null safe operator to try and access name, which will print nothing if it's not there.

Auth::user()?->name

But a more concrete and safer way is to have pages that require authentication under the auth middleware so Auth::user doesn't return null.

I'd suggest reading the Retrieving The Authenticated User section of the Authentication part of Laravel's official documentation.

This part of the documentation also covers Protecting Routes.

ookma-kyi

Aug 15th, 2023 05:47 PM

So why doesn't it crash when using the default Laravel welcome page template? For reference here is what it looks like.

thinkverse

Aug 15th, 2023 06:01 PM

Best Answer

It's because you're extending layouts.app, that's where the navigation.php component gets included, which contains the Auth::user() call.

Screenshot 2023-08-16 at 02.54.42.png

It's designed to be used for view that are accessed by authenticated users. Login and register uses the layouts.guest layout, which doesn't include any Auth calls.

Laravel's default welcome.blade.php doesn't include any layout, or Auth calls. It's independed from layouts.

Laravel covers layouts in its Blade documentation under Building Layouts that's worth a read.