title: Understanding Eloquent in Laravel
body: <p>Previously I have worked with bog standard PHP and have recently made the move over to Laravel. One thing I cannot grasp is SQL queries in Laravel Eloquent.</p>
  
  <p>For instance I have a <strong>roles</strong> and a <strong>users</strong> table. The roles table contains <strong>roles and IDs for each role</strong>. The users table contains various user details as well as a <strong>role_id to reference the role</strong> of the user.</p>
  
  <p>What I have tried in my <strong>user model</strong></p>
  
  <pre><code>public function role()
  {
      // return $this-&gt;hasOne('App\Phone', 'foreign_key', 'local_key');
      return $this-&gt;hasOne('App\Role', 'id', 'role_id');
  }
  </code></pre>
  
  <p>What I have tried in my <strong>role model</strong></p>
  
  <pre><code>public function users()
  {
      return $this-&gt;hasMany('App\User', 'role_id', 'id');
  }
  </code></pre>
  
  <p>Then in my <strong>controller</strong></p>
  
  <pre><code>public function index()
  {
      $var = User::find()-&gt;role-&gt;get();
  
      return view('article', 
          array(
          'var' =&gt; $var
          ));
  }
  </code></pre>
  
  <p>But now how do I access the fields in both tables as I would in ordinary PHP? I would rather use standard SQL but am trying to properly learn this framework.</p>
  
  <p>Any advice?</p>