title: MySQL (Laravel Eloquent) - Get record when two columns have two or more values at the same time
body: <p>I have this database that I got from <a href="https://stackoverflow.com/questions/24923469/modeling-product-variants">this post</a> that manages products and its variants:</p>
  
  <pre><code>+---------------+     +---------------+
  | PRODUCTS      |-----&lt; PRODUCT_SKUS  |
  +---------------+     +---------------+
  | #product_id   |     | #product_id   |
  |  product_name |     | #sku_id       |
  +---------------+     |  sku          |
          |             |  price        |
          |             +---------------+
          |                     |
  +-------^-------+      +------^------+
  | OPTIONS       |------&lt; SKU_VALUES  |
  +---------------+      +-------------+
  | #product_id   |      | #product_id |
  | #option_id    |      | #sku_id     |
  |  option_name  |      | #option_id  |
  +---------------+      |  value_id   |
          |              +------v------+
  +-------^-------+             |
  | OPTION_VALUES |-------------+
  +---------------+
  | #product_id   |
  | #option_id    |
  | #value_id     |
  |  value_name   |
  +---------------+
  </code></pre>
  
  <p>The problem is, that I don't know how would I get the SKU at the moment that a user selects the options of the product he wants:</p>
  
  <pre><code>SKU_VALUES
  ==========
  product_id sku_id option_id value_id
  ---------- ------ --------- --------
  1          1      1         1        (W1SSCW; Size; Small)
  1          1      2         1        (W1SSCW; Color; White)
  1          2      1         1        (W1SSCB; Size; Small)
  1          2      2         2        (W1SSCB; Color; Black)
  </code></pre>
  
  <p>Let's suppose that the user selects the product with ID 1 and the options size-small and color-black, how am I able to get the <code>sku_id</code> (in this case I would want value 2 from <code>sku_id</code>) in order to get the price that's inside the <code>PRODUCT_SKUS</code> table.</p>
  
  <p>I cannot do something like this for obvious reasons:</p>
  
  <pre><code>SELECT sku_id FROM SKU_VALUES 
  WHERE (SKU_VALUES.option_id = 1 AND SKU_VALUES.value_id = 1) 
  AND (SKU_VALUES.option_id = 2 AND SKU_VALUES.value_id = 2)
  </code></pre>
  
  <p>NOTE that it seems that I would need to append the same number of conditions (or whatever I need) as the number of options that are available from a product, in this case there are just 2 rows because the product has 2 options (size and color), but the product may have "n" options. </p>
  
  <p>I would appreciate if someone could guide me for this query and if it's possible doing it with Laravel Eloquent instead of using RAW query.</p>
  
  <p>The models I have created are the following:</p>
  
  <p>"Product" Model:</p>
  
  <pre><code>namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Producto extends Model
  {
      protected $table = 'productos';
  
      protected $fillable = [
          'nombre',
          'descripcion'
  
      ];
  
  public function opciones(){
      return $this-&gt;hasMany('App\Models\OpcionProducto', 'producto_id');
  }
  
  public function skus(){
      return $this-&gt;hasMany('App\Models\ProductoSku', 'producto_id');
  }
  }
  </code></pre>
  
  <p>"Options" Model:</p>
  
  <pre><code>namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  use App\Traits\HasCompositePrimaryKey;
  
  class OpcionProducto extends Model
  {
      use HasCompositePrimaryKey;
  
      protected $table = 'productos_opciones';
  
      protected $primaryKey = array('producto_id', 'opcion_id');
  
      protected $fillable = [
          'producto_id',
          'opcion_id',
          'nombre_opcion',
          'valor'
      ];
  
  
      public function producto(){
          return $this-&gt;belongsTo('App\Models\Producto', 'producto_id');
      }
  
      public function valores(){
          return $this-&gt;hasMany('App\Models\OpcionValorProducto', 'opcion_id', 'opcion_id');
      }
  
      public function skusValores(){
          return $this-&gt;hasMany('App\Models\SkuValor', 'opcion_id', 'opcion_id');
      }
  }
  </code></pre>
  
  <p>"OptionValues" Model:</p>
  
  <pre><code>namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  use App\Traits\HasCompositePrimaryKey;
  
  class OpcionValorProducto extends Model
  {
      use HasCompositePrimaryKey;
  
      protected $primaryKey = array('producto_id', 'opcion_id', 'valor_id');
  
      protected $table = 'productos_opciones_valores';
  
      protected $fillable = [
          'producto_id',
          'opcion_id',
          'valor_id',
          'valor_variacion',
          'valor'
      ];
  
  
      public function producto(){
          return $this-&gt;belongsTo('App\Models\Producto', 'producto_id');
      }
  
      public function opcion(){
          return $this-&gt;belongsTo('App\Models\OpcionProducto', 'opcion_id', 'opcion_id');
      }
  }
  </code></pre>
  
  <p>"Product_SKUS" model:</p>
  
  <pre><code>namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  use App\Traits\HasCompositePrimaryKey;
  
  class ProductoSku extends Model
  {
      use HasCompositePrimaryKey;
  
      protected $primaryKey = array('producto_id', 'sku_id');
  
      protected $table = 'productos_skus';
  
      protected $fillable = [
          'producto_id',
          'sku_id',
          'imagen_id',
          'precio',
          'stock',
          'sku'
      ];
  
  
      public function producto(){
          return $this-&gt;belongsTo('App\Models\Producto', 'producto_id');
      }
  
      public function valoresSku(){
          return $this-&gt;hasMany('App\Models\SkuValor', 'sku_id');
      }
  
      }
  }
  </code></pre>
  
  <p>"SKU_VALUES" model:</p>
  
  <pre><code>namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  use App\Traits\HasCompositePrimaryKey;
  
  class SkuValor extends Model
  {
      use HasCompositePrimaryKey;
  
      protected $primaryKey = array('producto_id', 'sku_id', 'opcion_id');
  
      protected $table = 'valores_skus';
  
      protected $fillable = [
          'producto_id',
          'sku_id',
          'opcion_id',
          'valor_id',
      ];
  
  
      public function producto(){
          return $this-&gt;belongsTo('App\Models\Producto', 'producto_id');
      }
  
      public function opcion(){
          return $this-&gt;belongsTo('App\Models\OpcionProducto', 'opcion_id', 'opcion_id');
      }
  
      public function sku(){
          return $this-&gt;belongsTo('App\Models\ProductoSku', 'sku_id', 'sku_id');
      }
  }
  </code></pre>