Laravel access authenticated user in global scope
<p>I have a multi-user application with the following entity/model relationship. </p>

<p><strong>User</strong> belongs to a <strong>Company</strong> and has many <strong>SupportTicket</strong></p>

<p>Users of one company should not have access to the support tickets of other companies - if a user of one company navigates to a url pointing to a support ticket of another company, they should receive a 404 or something equivalent. </p>

<p>I was hoping to define a global scope in the SupportTicket class as follows: </p>

<pre><code>class SupportTicket extends Model {

    protected $fillable = ['company_id'];

    public static function boot() {
        static::addGlobalScope('ofCompany', function(Builder $builder) {
            $authUser = auth()-&gt;user();

            $builder-&gt;where('company_id', $authUser-&gt;company_id);
        });
    }

}
</code></pre>

<p>The benefit of a global scope is that it will apply to all queries. Other developers working on the project need not work when retrieving tickets as the right results will always be returned. </p>

<p>For example <code>SupportTicket::all()</code> or <code>SupportTicket::find(5)</code> will always restrict access by user's company. </p>

<p>The challenge I have now is that <code>auth()-&gt;user()</code> always returns null as the authenticate middleware has not yet been run. </p>

<ol>
<li>Is there a way around this? </li>
<li>Has anyone here faced a similar problem and if so how did they go around it? </li>
<li>Is it possible to access the authenticated user in the boot method where the global scope is registered?</li>
</ol>

<p>Many thanks in advance for any and all responses. </p>