title: Laravel Eloquent: get model id from inside getAttribute function?
body: <p>I have a model like the following:</p>
  
  <pre><code>User extends Authenticatable 
  {
     protected $appends = array('role');
  
     public function getRoleAttribute()
     {
        $role = DB::table('acl_user_has_roles')-&gt;where('user_id', $this-&gt;id)
                  -&gt;value('role');
        return $role;
     }
   }
  </code></pre>
  
  <p>When I try to reference this foo attribute like the following:</p>
  
  <pre><code>$user = User::find(1);
  unset($user-&gt;id);   // This line causes the problem.
  echo $user-&gt;role;
  </code></pre>
  
  <p>I always get "null" instead of expected "Owner".</p>
  
  <p>What did I miss here?</p>
  
  <p>I am running this in laravel 5.5.43.</p>
  
  <p>In the <a href="https://stackoverflow.com/questions/40712755/laravel-get-model-id-inside-setattribute">following post</a>, getKey() function was mentioned, that didn't work.</p>
  
  <p>The problem is actually caused by something else, as described in the following <a href="https://medium.com/@lynnlin827/be-careful-to-use-laravel-eloquents-accessors-mutators-and-events-45d820f54b8e" rel="nofollow noreferrer">medium post</a>.</p>