title: Laravel Global Scopes : ErrorException Undefined property: App\Models\User::$company
body: <p>I am working on a multi company application and I want to filter the data based on which type of user is logged in .. [ Head Office Admin or Standard User ]</p>
  <p>So I want head office to be able to see all branch companies reports, so in the head office company profile, I auto generated a code on registration. So all branches registered after need that code so they are linked up to head office.</p>
  <p>This is the error I get.</p>
  <pre><code>ErrorException
  Undefined property: App\Models\User::$company
  </code></pre>
  <p>I want to setup a global scope so head office admin can access branch companies, including data.</p>
  <p><em>/ Company Model Global Scope /</em></p>
  <pre><code>
      public static function boot()
      {
          parent::boot();
  
          if(Auth::user()-&gt;user_role_id &gt;= 4 &amp;&amp; Auth::user()-&gt;access_hq == 'YES'){
              static::addGlobalScope('hq_code', function (Builder $builder) {
                  $builder-&gt;where('hq_code', Auth::user()-&gt;company['hq_code']);
              });
          } elseif(Auth::user()-&gt;user_role_id &gt;= 4 &amp;&amp; Auth::user()-&gt;access_hq !== 'YES'){
              static::addGlobalScope('id', function (Builder $builder) {
                  $builder-&gt;where('id', Auth::user()-&gt;company_id);
              });
          }
      }
  </code></pre>
  <p><em>/ Model Relationships /</em></p>
  <pre><code>/**
  * User Model
  */
      public function company()
      {
          return $this-&gt;belongsTo(\App\Models\Company::class);
      }
  
  /**
  * Company Model
  */
  
      public function users()
      {
          return $this-&gt;hasMany(\App\Models\User::class);
      }
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>If I debug the query logged in as Admin/HQ Admin I still get the error, but when I am logged in as branch user/standard user I get this</p>
  <pre><code>Illuminate\Database\Eloquent\Builder {#1955 ▼
    #query: Illuminate\Database\Query\Builder {#1962 ▶}
    #model: App\Models\Company {#1948 ▶}
    #eagerLoad: []
    #localMacros: array:4 [▶]
    #onDelete: Closure(Builder $builder) {#1944 ▶}
    #passthru: array:19 [▶]
    #scopes: array:2 [▼
      &quot;Illuminate\Database\Eloquent\SoftDeletingScope&quot; =&gt; Illuminate\Database\Eloquent\SoftDeletingScope {#1765 …}
      &quot;id&quot; =&gt; Closure(Builder $builder) {#1854 ▼
        class: &quot;App\Models\Company&quot;
        file: &quot;DRIVE:\webapp\app\Models\Company.php&quot;
        line: &quot;70 to 72&quot;
      }
    ]
    #removedScopes: []
  }
  </code></pre>
  <p><strong>Edit - dd(auth()-&gt;user())</strong></p>
  <pre><code>App\Models\User {#1938 ▼
    #fillable: array:4 [▶]
    #hidden: array:2 [▶]
    #attributes: array:17 [▶]
    #connection: &quot;mysql&quot;
    #table: &quot;users&quot;
    #primaryKey: &quot;id&quot;
    #keyType: &quot;int&quot;
    +incrementing: true
    #with: []
    #withCount: []
    +preventsLazyLoading: false
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #original: array:17 [▶]
    #changes: []
    #casts: array:1 [▶]
    #classCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [▶]
    #rememberTokenName: &quot;remember_token&quot;
    #forceDeleting: false
  }
  </code></pre>