title: Laravel 8: Attempt to read property "id" on null
body: <p>I am facing this error 'Attempt to read property &quot;id&quot; on null' in Laravel 8. It was working fine before but in my view I changed $user-&gt;id to $user-&gt;profile-&gt;id and now this is happening. I am logged in the app and I have changed my route accordingly to match profile id, I have also tried clearing cache etc.</p>
  <p>Here is my Code:</p>
  <p>User Model:</p>
  <pre><code>    class User extends Authenticatable
  {
      use HasApiTokens, HasFactory, Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var string[]
       */
  //    protected $fillable = [
  //        'name',
  //        'email',
  //        'password',
  //    ];
  
      protected $table = 'users';
      protected $guarded = [];
  
      /**
       * The attributes that should be hidden for serialization.
       *
       * @var array
       */
      protected $hidden = [
          'password',
          'remember_token',
      ];
  
      /**
       * The attributes that should be cast.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
  
      public function setPasswordAttribute($password)
      {
          $this-&gt;attributes['password'] = bcrypt($password);
      }
  
      public function posts ()
      {
          return $this-&gt;hasMany(Post::class);
      }
  
      public function profile()
      {
          return $this-&gt;hasOne(Profile::class);
      }
  }
  </code></pre>
  <p>Profile Model:</p>
  <pre><code>class Profile extends Model
  {
      use HasFactory;
  
      protected $table = 'profiles';
      protected $guarded = [];
  
      public function user()
      {
          return $this-&gt;belongsTo(User::class);
      }
  }
  </code></pre>
  <p>ProfilesController:</p>
  <pre><code>class ProfilesController extends Controller
  {
      public function show(User $user)
      {
          return view ('profiles.index', compact('user'));
      }
      
      public function edit(User $user)
      {
          return view ('profiles.index', compact('user'));
      }
  }
  </code></pre>
  <p>Route:</p>
  <pre><code>    Route::get('profile/{profile}', [ProfilesController::class, 'show'])-&gt;middleware('auth');
  Route::get('profile/{profile}/edit', [ProfilesController::class, 'edit'])-&gt;middleware('auth');
  </code></pre>
  <p>View:</p>
  <pre><code>&lt;x-layout&gt;
  &lt;section class=&quot;py-8 max-w-4xl mx-auto&quot;&gt;
      &lt;h1 class=&quot;text-lg font-bold mb-8 pb-2 border-b&quot;&gt;
          @if ($user-&gt;id == auth()-&gt;user()-&gt;id)
          Hello {{ $user-&gt;name }}, welcome to your profile.
          @else
          {{ $user-&gt;name }}'s Profile.
          @endif
      &lt;/h1&gt;
      &lt;div class=&quot;flex&quot;&gt;
          &lt;aside class=&quot;w-48 flex-shrink-0&quot;&gt;
              &lt;h4 class=&quot;font-semibold mb-4&quot;&gt;
                  Navigation
              &lt;/h4&gt;
              &lt;ul style=&quot;max-width: 75%&quot;&gt;
                  &lt;li&gt;
                      &lt;a href=&quot;/profile/{{ $user-&gt;profile-&gt;id }}&quot; class=&quot;{{ request()-&gt;is('profile/'.$user-&gt;profile-&gt;id) ? 'text-blue-500' : '' }}&quot;&gt;View Profile&lt;/a&gt;
                  &lt;/li&gt;
                  &lt;li&gt;
                          @if ($user-&gt;id == auth()-&gt;user()-&gt;id)
                              &lt;a href=&quot;/profile/{{ $user-&gt;profile-&gt;id }}/edit&quot; class=&quot;{{ request()-&gt;is('profile/'.$user-&gt;profile-&gt;id.'/edit') ? 'text-blue-500' : '' }}&quot;&gt;Edit Profile&lt;/a&gt;
                          @endif
                  &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/aside&gt;
          &lt;main class=&quot;flex-1&quot;&gt;
              &lt;x-panel&gt;
                  &lt;div class=&quot;flex flex-col&quot;&gt;
                      &lt;div class=&quot;-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8&quot;&gt;
                          &lt;div class=&quot;py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8&quot;&gt;
                              &lt;div class=&quot;shadow overflow-hidden border-b border-gray-200 sm:rounded-lg&quot;&gt;
                                  &lt;div class=&quot;flex flex-row grid-cols-12&quot;&gt;
                                      &lt;div class=&quot;flex flex-col col-span-4 row-span-full justify-items-start flex-grow-0 flex-shrink-0 grid-cols-4&quot;&gt;
                                          &lt;div class=&quot;flex flex-row&quot;&gt;
                                              &lt;img src=&quot;http://i.pravatar.cc/60?u={{ $user-&gt;profile-&gt;id }}&quot; alt=&quot;&quot; width=&quot;&quot; height=&quot;&quot; class=&quot;rounded-full h-24 w-24 flex m-2&quot;&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;flex flex-col col-span-8 justify-right grid-cols-8 text-sm&quot;&gt;
                                          &lt;div class=&quot;flex flex-row&quot;&gt;
                                              &lt;div class=&quot;flex flex-col col-span-2 font-semibold&quot;&gt;
                                                  &lt;div class=&quot;pt-3&quot;&gt;Name:&lt;/div&gt;
                                                  &lt;div class=&quot;pt-3&quot;&gt;Email:&lt;/div&gt;
                                                  &lt;div class=&quot;pt-3&quot;&gt;About:&lt;/div&gt;
                                              &lt;/div&gt;
                                              &lt;div class=&quot;flex flex-col col-span-10&quot;&gt;
                                                  &lt;div class=&quot;pt-3 pl-4 text-justify&quot;&gt;{{ $user-&gt;profile-&gt;name }}&lt;/div&gt;
                                                  &lt;div class=&quot;pt-3 pl-4 text-justify&quot;&gt;{{ $user-&gt;profile-&gt;email }}&lt;/div&gt;
                                                  &lt;div class=&quot;pt-3 pl-4 text-justify&quot;&gt;&lt;p&gt;{{ $user-&gt;profile-&gt;description }}&lt;/p&gt;&lt;/div&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/x-panel&gt;
          &lt;/main&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>