title: Return value must be of type ?Illuminate\\Database\\Query\\Builder, App\\Models\\ModelName returned
body: <p>I'm trying to have the following response:</p>
  <pre><code>&quot;user&quot;: {
          &quot;id&quot;: 1,
          &quot;first_name&quot;: &quot;john&quot;,
          &quot;last_name&quot;: &quot;doe&quot;,
          &quot;email&quot;: &quot;john@mail.com&quot;,
          &quot;phone_number&quot;: &quot;12345678&quot;,
          &quot;email_verified_at&quot;: null,
          &quot;created_at&quot;: &quot;2021-09-02T08:57:07.000000Z&quot;,
          &quot;updated_at&quot;: &quot;2021-09-02T08:57:07.000000Z&quot;,
          &quot;country&quot;: {
                       &quot;id&quot;: 1,
                       &quot;name&quot;: &quot;UK&quot;,
                       &quot;phone_code&quot;: 44
                      } 
      }
  </code></pre>
  <p>Instead of:</p>
  <pre><code>&quot;user&quot;: {
          &quot;id&quot;: 1,
          &quot;first_name&quot;: &quot;john&quot;,
          &quot;last_name&quot;: &quot;doe&quot;,
          &quot;email&quot;: &quot;omar.fd.du@gmail.com&quot;,
          &quot;phone_number&quot;: &quot;12345678&quot;,
          &quot;email_verified_at&quot;: null,
          &quot;created_at&quot;: &quot;2021-09-02T08:57:07.000000Z&quot;,
          &quot;updated_at&quot;: &quot;2021-09-02T08:57:07.000000Z&quot;,
          &quot;country_id&quot;: 1
      }
  </code></pre>
  <p>In order to do that, I am using a mutator function inside the User Model:</p>
  <pre><code>public function getCountryIdAttribute(): Builder|null
  {
     return Country::where('id', $this-&gt;attributes['country_id'])
      -&gt;get()
      -&gt;first();
  }
  </code></pre>
  <p>However, the countries table is already found in an external database which I set up its connection properly.</p>
  <p>But I created the Country Model following this <a href="https://laravel.com/docs/8.x/eloquent" rel="nofollow noreferrer">Laravel documentation</a>:</p>
  <pre><code>use Illuminate\Database\Eloquent\Model;
  
  class Country extends Model
  {
  /**
   * The database connection that should be used by the model.
   *
   * @var string
   */
  protected $connection = 'my second db connection name';
  
  /**
   * The table associated with the model.
   *
   * @var string
   */
  protected $table = 'countries';
  
  /**
   * The primary key associated with the table.
   *
   * @var string
   */
  protected $primaryKey = 'id';
  
  /**
   * The model's default values for attributes.
   *
   * @var array
   */
  protected $attributes = [
      'id',
      'name',
      'phone_code',
  ];
  }
  </code></pre>
  <p>When I am trying to fetch the user I get the following error:</p>
  <pre><code>{
  &quot;error&quot;: [
      &quot;App\\Models\\User::getCountryIdAttribute(): Return value must be of type ? 
      Illuminate\\Database\\Query\\Builder, App\\Models\\Country returned&quot;
  ],
  &quot;message&quot;: &quot;Unhandled server exception&quot;,
  &quot;code&quot;: 500
  }
  </code></pre>
  <p>I tried to explain my case as much as possible.
  Thank you for your help.</p>