title: Accessing Pivot table "3rd Model relationship" in Laravel Eloquent
body: <p>I have this Many-to-Many relation Laravel Eloquent relationship
  The Models are:</p>
  
  <pre><code>class Email extends Model //actually represent the email account
  {
      protected $table = 'emails';
  
      protected $fillable = [
          'user_id',
          'name',
      ];
  
      public function messages() {
         return $this-&gt;belongsToMany(Message::class)-&gt;withPivot('email_subtype_id');
      }
  }
  
  class Message extends Model //actually represent the email message
  {
      protected $table = 'messages';
  
      protected $fillable = [
          'subject',
          'body ',
      ];
  
      public function emails() {
          return $this-&gt;belongsToMany(Email::class)-&gt;withPivot('email_subtype_id');
      }
  }
  
  class EmailMessage extends Pivot //actually represent the pivot table
  {
      protected $table = 'email_message';
  
      protected $fillable = [
          'email_id',
          'message_id',
          'email_subtype_id',
      ];
  
      public function email() {
          return $this-&gt;belongsTo(Email::class);
      }
  
      public function message() {
          return $this-&gt;belongsTo(Message::class);
      }
  
      //this is the relation to a third model called EmailSubtype
      //I want to include this relation to the Pivot when using it
      public function subtype() {
          return $this-&gt;belongsTo(EmailSubtype::class, 'email_subtype_id');
      }
  }
  
  class EmailSubtype extends Model //3rd Model need to be included with Pivot
  {
      protected $table = 'email_subtypes';
  
      protected $fillable = [
          'name'
      ];
  
      public function pivotEmailSubtype(){
          return $this-&gt;hasMany(Pivot::class, 'email_subtype_id');
      }
  }
  </code></pre>
  
  <p>I am able to do this in the Controller:</p>
  
  <pre><code>    $email = Email::find(1);
  
      foreach($email-&gt;messages as $message) {
          $subtype_id = $message-&gt;pivot-&gt;email_subtype_id;
          dd($subtype_id); //1 that relates to subtype: CC Email Account
          //also I can get the name indirectly away from the relation as follows:
          $subtypeName = EmailSubtype::find($subtype_id)-&gt;first()-&gt;name;
          dd($subtypeName);
      }
  </code></pre>
  
  <p>Here I am getting the email_subtype_id only by direct pivot relation but have to do extra work to get the related email sub-type name.</p>
  
  <p>I need to get the email sub-type name [Directly] from the 3rd Model EmailSubtype relationship oneToMany [hasMany and belongsTo] that relates to Pivot model with EmailSubtype model using something like:</p>
  
  <pre><code>$message-&gt;pivot-&gt;subtypeName;
  </code></pre>
  
  <p>Any help please!</p>