title: Laravel Multiple Table Subquery
body: <p>I'm using Laravel 6.0
  I have 4 different tables.</p>
  <p>Tables:</p>
  <blockquote>
  <p><strong>post_category</strong></p>
  </blockquote>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>relation_post_id</th>
  <th>post_type</th>
  <th>category_id</th>
  </tr>
  </thead>
  </table>
  </div>
  <blockquote>
  <p><strong>photo_post</strong></p>
  </blockquote>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>title</th>
  <th>image</th>
  <th>created_at</th>
  </tr>
  </thead>
  </table>
  </div>
  <blockquote>
  <p><strong>text_post</strong></p>
  </blockquote>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>title</th>
  <th>content</th>
  <th>created_at</th>
  </tr>
  </thead>
  </table>
  </div>
  <blockquote>
  <p><strong>video_post</strong></p>
  </blockquote>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>title</th>
  <th>video_source_url</th>
  <th>created_at</th>
  </tr>
  </thead>
  </table>
  </div>
  <p>I'd like to list posts in two date ranges from the post_category table. For example</p>
  <pre><code>PostCategory::whereBetween('created_at',[$from, $to])-&gt;get();
  </code></pre>
  <p>The result should be:</p>
  <p>Result</p>
  <blockquote>
  <p><a href="https://i.ibb.co/y53PmJ9/image.png" rel="nofollow noreferrer">https://i.ibb.co/y53PmJ9/image.png</a></p>
  </blockquote>
  <p>How can I do that?</p>
  <p>Models:
  PostCategory</p>
  <pre><code>class PostCategory extends Model
  {
  use SoftDeletes;
  protected $table = 'post_category';
  public $timestamps = true;
  
  protected $fillable = [
      'relation_post_id',
      'post_type', // 1: text, 2: photo, 3: video
      'category_id',
  
  
  ];
  
  public function text()
  {
      return $this-&gt;belongsTo('App\TextPost','relation_post_id','id');
  }
  public function photo()
  {
      return $this-&gt;belongsTo('App\PhotoPost','relation_post_id','id');
  }
  public function video()
  {
      return $this-&gt;belongsTo('App\VideoPost','relation_post_id','id');
  }}
  </code></pre>
  <p>TextPost</p>
  <pre><code>class TextPost extends Model
  {
  use SoftDeletes;
  protected $table = 'text_post';
  public $timestamps = true;
  
  protected $fillable = [
      'title',
      'content',
  ];
  }
  </code></pre>
  <p>PhotoPost</p>
  <pre><code>class PhotoPost extends Model
  {
  use SoftDeletes;
  protected $table = 'photo_post';
  public $timestamps = true;
  
  protected $fillable = [
      'title',
      'image',
  
  ];
  }
  </code></pre>
  <p>Video Post</p>
  <pre><code>class VideoPost extends Model
  {
  use SoftDeletes;
  protected $table = 'video_post';
  public $timestamps = true;
  
  protected $fillable = [
      'title',
      'video_source_url',
  
  ];
  }
  </code></pre>