title: Null Value On One-to-One Relationships Laravel 7
body: <p>I have an One-to-One relationships Between the user (penggunas) table and the role table, but when I access it in the controller it returns null and column not found</p>
  <p>Route.php</p>
  <pre><code>Route::get('/test', 'PenggunaController@index');
  </code></pre>
  <p>PenggunaController.php</p>
  <p>use App\Pengguna;</p>
  <pre><code>class PenggunaController extends Controller
  {
      public function index()
      {
          $pengguna = Pengguna::find(3);
          return response()-&gt;json([
              'data' =&gt; $pengguna
          ]);
      }
  }
  </code></pre>
  <p>Migrations</p>
  <p>create_penggunas_table</p>
  <pre><code>public function up()
  {
      Schema::create('penggunas', function (Blueprint $table) {
          $table-&gt;id();
          $table-&gt;string('username');
          $table-&gt;unsignedBigInteger('role_id');
  
          $table-&gt;foreign('role_id')-&gt;references('id')-&gt;on('roles');
      });
  }
  </code></pre>
  <p>create_roles_table</p>
  <pre><code>public function up()
  {
      Schema::create('roles', function (Blueprint $table) {
          $table-&gt;id('id');
          $table-&gt;string('role_name')-&gt;unique();
      });
  }
  </code></pre>
  <p>Model</p>
  <p>Pengguna.php</p>
  <pre><code>public function role()
  {
      return $this-&gt;hasOne('App\Role');
  }
  </code></pre>
  <p>Role.php</p>
  <pre><code>public function pengguna()
  {
      return $this-&gt;belongsTo('App\Pengguna');
  }
  </code></pre>
  <p>So, on the method index at PenggunaController.php I am fetching data which have id = 3. It's return full data, the role_id shows up, but if I access it like <code>$pengguna_role = $pengguna-&gt;role-&gt;role_name;</code>, it returns error with message column not found. Then I tried to change to <code>$pengguna-&gt;roles-&gt;role_name;</code>, it returns error with message 'not an object', I tried <code>dd($penggunas-&gt;roles);</code> and it's returning null.</p>
  <blockquote>
  <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.pengguna_id' in 'where clause' (SQL: select * from <code>roles</code> where <code>roles</code>.<code>pengguna_id</code> = 3 and <code>roles</code>.<code>pengguna_id</code> is not null limit 1)</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/feHBk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/feHBk.png" alt="roles table" /></a></p>
  <p><a href="https://i.stack.imgur.com/MG2NJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MG2NJ.png" alt="penggunas table" /></a></p>
  <p>I can get around this by taking the role_id in the result from <code>User::find(3);</code> and then <code>Role::where('id', $pengguna-&gt;role_id);</code>. But it makes me confused why I can't access it directly using relationship.</p>