title: laravel belongsTo where
body: <p>I want data from table_entrypoint over table_a all rows from table_b with filtered table_c.</p>
  
  <p>Tables:</p>
  
  <pre><code>table_entrypoint:
      id - int
  
  table_a:
      id - int
      table_entrypoint_id - int
      table_b_id - int
      table_c_id - int|NULL
  
  table_b:
      id - int
  
  table_c:
      id - int
      table_b_int
  </code></pre>
  
  <p>table_entrypoint has many table_a. table_a has one table_b. table_b hast many table_c.</p>
  
  <p>Something like <code>Entrypoint::first()-&gt;belongsTo('table_a')-&gt;with('table_b')-&gt;with('table_c')-&gt;get()</code> will create an <code>Illuminate\Database\Eloquent\Collection</code> for table_c. I want filterd by table_a.table_c_id an <code>Illuminate\Database\Eloquent\Model</code>. I prefered a function in table_a if there is no logic problems.</p>
  
  <p>Here comes something from my code ...</p>
  
  <p>My Model (hard cutted down) for table_a:</p>
  
  <pre><code>class Order extends Illuminate\Database\Eloquent\Model
  {
      protected $fillable = [
          'product_id',
          'variant_id',
          'table_id',
      ];
  
      /**
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function product()
      {
          return $this-&gt;belongsTo(Product::class);
      }
  
      /**
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function variant()
      {
          return $this-&gt;belongsTo(Variant::class);
      }
  
      /**
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function table()
      {
          return $this-&gt;belongsTo(Table::class);
      }
  }
  </code></pre>
  
  <p>My Model (hard cutted down) for table_b:</p>
  
  <pre><code>class Product extends Illuminate\Database\Eloquent\Model
  {
      protected $fillable = [
          'name',
      ];
  
      protected $with = [
          'variants',
      ];
  
      /**
       * @return \Illuminate\Database\Eloquent\Relations\HasMany
       */
      public function variants()
      {
          return $this-&gt;hasMany(Variant::class);
      }
  
      /**
       * @return \Illuminate\Database\Eloquent\Relations\HasMany
       */
      public function orders()
      {
          return $this-&gt;hasMany(Order::class);
      }
  }
  </code></pre>
  
  <p>My Model (hard cutted down) for table_c:</p>
  
  <pre><code>class Variant extends Illuminate\Database\Eloquent\Model
  {
      protected $fillable = [
          'product_id',
          'name',
      ];
  
      /**
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function product()
      {
          return $this-&gt;belongsTo(Product::class);
      }
  }
  </code></pre>
  
  <p>If i call on tinker <code>App\Table::first()-&gt;orders()-&gt;with('product')-&gt;get()</code> i get the following result:</p>
  
  <pre><code>=&gt; Illuminate\Database\Eloquent\Collection {#712
       all: [
         App\Order {#711
           id: "1",
           product: App\Product {#725
             id: "1",
             name: "TEST",
             variants: Illuminate\Database\Eloquent\Collection {#735
               all: [
                 App\Variant {#740
                   id: "1",
                   name: "Variant1",
                 },
                 App\Variant {#741
                   id: "2",
                   name: "Variant2",
                 },
                 [...]
               ],
             },
           },
         },
       ],
     }
  </code></pre>
  
  <p>I want something like <code>App\Table::first()-&gt;orders()-&gt;with('productVariant')-&gt;get()</code> with a result of:</p>
  
  <pre><code>=&gt; Illuminate\Database\Eloquent\Collection {#712
       all: [
         App\Order {#711
           id: "1",
           product: App\Product {#725
             id: "1",
             name: "TEST",
             variant: App\Variant {#740
                 id: "1",
                 name: "Variant1",
               },
             },
           },
         },
       ],
     }
  </code></pre>
  
  <p>Is there a function in laravel <code>Illuminate\Database\Eloquent</code> for this?</p>