title: Laravel another way of appending to model
body: <p>I'm building an api Laravel. In my message model I've append the following:</p>
  
  <pre><code>/**
   * @var array
   */
  protected $appends = ['subscribed'];
  
  
  /**
   * @return mixed
   */
  public function getSubscribedAttribute()
  {
      return $this-&gt;isSubscribedBy();
  }
  </code></pre>
  
  <p><code>IsSubscribedBy();</code> lives in a trait that looks like this:</p>
  
  <pre><code>/**
   * @return mixed
   */
  public function isSubscribedBy()
  {
      return $this-&gt;subscriptions()
          -&gt;where('user_id', Auth::user()-&gt;id)
          -&gt;exists();
  }
  </code></pre>
  
  <p>This is working but doing it like this has the <code>n + 1</code> problem and I can't load the messages in algolia with <code>php artisan scout:import "App\Messages\Message"</code> that obviously throws an error (because no one is signed in but it's looking for someone: <code>Auth::user()-&gt;id):</code></p>
  
  <blockquote>
    <p>[ErrorException]<br>
     Trying to get property of non-object</p>
  </blockquote>
  
  <p>So are there better ways? I already tried fractal but then I still have the <code>n + 1</code> problem.</p>
  
  <p>---EDIT---</p>
  
  <p>I'm returning like this:</p>
  
  <pre><code>/**
   * @return mixed
   */
  public function index()
  {
      return Message::orderBy('created_at', 'DESC')
          -&gt;paginate(10);
  }
  </code></pre>
  
  <p>So for each message it will exec this query:</p>
  
  <pre><code>$this-&gt;subscriptions()
          -&gt;where('user_id', Auth::user()-&gt;id)
          -&gt;exists();
  </code></pre>