Error Modifying welcome.blade.php
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.
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.
It's because you're extending layouts.app
, that's where the navigation.php component gets included, which contains the Auth::user()
call.
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.