title: Attempt to read property "username" on null
body: <p>While trying to create fake posts on laravel 8, I met with some errors, first it wasn't creating the posts, then I changed the username to nullable and it created it but I keep having;</p>
  <p>Attempt to read property &quot;username&quot; on null</p>
  <p>So, I went back to my database and I changed it back to none, but I still receive the same error code, I will post my codes now...</p>
  <p>index.blade.php</p>
  <pre><code>@extends('layouts.app')
  
  @section('content')
  
      &lt;div class=&quot;flex justify-center&quot;&gt;
          &lt;div class=&quot;w-8/12 bg-white p-6 rounded-lg&quot;&gt;
              &lt;form action=&quot;{{ route('posts') }}&quot; method=&quot;post&quot; class=&quot;mb-4&quot;&gt;
                  @csrf
                  &lt;div class=&quot;mb-4&quot;&gt;
                      &lt;label for=&quot;body&quot; class=&quot;sr-only&quot;&gt;Body&lt;/label&gt;
                      &lt;textarea name=&quot;body&quot; id=&quot;body&quot; cols=&quot;30&quot; rows=&quot;4&quot; class=&quot;by-gray-100 border-2
                      w-full p-4 rounded lg @error('body') border-red-500 @enderror&quot; placeholder=&quot;Post something!&quot;&gt;&lt;/textarea&gt;
                      @error('body')
                          &lt;div class=&quot;text-red-500 mt-3 text-sm&quot;&gt;
                              {{$message}}
                          &lt;/div&gt;
                      @enderror
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;bg-blue-500 text-white px-4 py-2 rounded
                      font-medium&quot;&gt;Post&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
              @if ($posts-&gt;count())
                  @foreach ($posts as $post)
                      &lt;div class=&quot;mb-4&quot;&gt;
                          &lt;a href=&quot;&quot; class=&quot;font-bold&quot;&gt;{{ $post-&gt;user-&gt;username }}&lt;/a&gt;
                          &lt;span class=&quot;text-gray-600 text-sm&quot;&gt;{{ $post-&gt;created_at-&gt;diffForHumans() }}&lt;/span&gt;
                          &lt;p class=&quot;mb-2&quot;&gt;{{ $post-&gt;body }}&lt;/p&gt;
                      &lt;/div&gt;
  
                  @endforeach
  
                  {{ $posts-&gt;links() }}
  
              @else
                  There are no posts...
              @endif
          &lt;/div&gt;
      &lt;/div&gt;
  
  @endsection
  </code></pre>
  <p>UserFactory.php</p>
  <pre><code>&lt;?php
  
  namespace Database\Factories;
  
  use App\Models\User;
  use Illuminate\Database\Eloquent\Factories\Factory;
  use Illuminate\Support\Str;
  
  class UserFactory extends Factory
  {
      /**
       * The name of the factory's corresponding model.
       *
       * @var string
       */
      protected $model = User::class;
  
      /**
       * Define the model's default state.
       *
       * @return array
       */
      public function definition()
      {
          return [
              'username' =&gt; $this-&gt;faker-&gt;username,
              'name' =&gt; $this-&gt;faker-&gt;name,
              'email' =&gt; $this-&gt;faker-&gt;unique()-&gt;safeEmail,
              'email_verified_at' =&gt; now(),
              'password' =&gt; '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
              'remember_token' =&gt; Str::random(10),
          ];
      }
  
      /**
       * Indicate that the model's email address should be unverified.
       *
       * @return \Illuminate\Database\Eloquent\Factories\Factory
       */
      public function unverified()
      {
          return $this-&gt;state(function (array $attributes) {
              return [
                  'email_verified_at' =&gt; null,
              ];
          });
      }
  }
  </code></pre>
  <p>User.php</p>
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  
  class User extends Authenticatable
  {
      use HasFactory, Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name',
          'email',
          'password',
          'username',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password',
          'remember_token',
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
  
      public function posts(){
          return $this-&gt;hasMany(Post::class);
      }
  }
  </code></pre>
  <p>PostFactory.php</p>
  <pre><code>&lt;?php
  
  namespace Database\Factories;
  
  use App\Models\Post;
  use Illuminate\Database\Eloquent\Factories\Factory;
  
  class PostFactory extends Factory
  {
      /**
       * The name of the factory's corresponding model.
       *
       * @var string
       */
      protected $model = Post::class;
  
  
      /**
       * Define the model's default state.
       *
       * @return array
       */
      public function definition()
      {
          return [
              'body' =&gt; $this-&gt;faker-&gt;sentence(20),
                  ];
      }
  }
  </code></pre>
  <p>Post.php</p>
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Database\Eloquent\Model;
  
  class Post extends Model
  {
      use HasFactory;
  
      protected $fillable= [
          'body',
          'user_id',
      ];
  
     /**
       * Get the user that owns the Post
       *
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function user()
      {
          return $this-&gt;belongsTo(User::class);
      }
  }
  </code></pre>