1
私はアイテムの情報を含むアイテムのテーブルを持っています。さらに、私は2つ以上のテーブルを持つことができるいくつかの特性のための3つのテーブルを持っています。laravelの関係を使用してクエリをフィルタリングするにはどうすればいいですか
は例に従う:
table items
-----------------------
| id | price | stock |
-----------------------
| 1 | 19 | 99 |
-----------------------
table tallas
-----------------------------
| id | item_id| description |
-----------------------------
| 1 | 1 | large |
-----------------------------
table colors
-----------------------------
| id | item_id| description |
-----------------------------
| 1 | 1 | blue |
-----------------------------
table materials
-----------------------------
| id | item_id| description |
-----------------------------
| 1 | 1 | cotton |
-----------------------------
私は(例えば)join
を使用せずにtallas
関係にitem_id
を渡すアイテムをフィルタリングすることができる方法があるかどうかを知りたいです。
は私のモデルは次のようになります。
<?php
class Item extends Eloquent{
use Conner\Tagging\TaggableTrait;
public function tallas()
{
return $this->hasMany('Talla','item_id');
}
public function colores()
{
return $this->hasMany('Color','item_id');
}
public function materiales()
{
return $this->hasMany('Material','item_id');
}
public function imagenes()
{
return $this->hasMany('ItemImage','item_id');
}
}
そして、私はこの試みている:私は探している場合の例テーブルを使用して、tallas
を
$items = Item::with('imagenes')
->with(array('tallas' => function($query) use($dataFilter) {
$query->where('id','=',$dataFilter['filter-size']);
}));
しかし、このリターンは、すべてのアイテムを、フィルター項目small
このようなものが返されます。
[item]{
id:1,
price:19,
stock:99,
tallas:
[]...
colores:
[]...
materiales:
[]...
}
どのようなヘルプも表示されません。
EDITED
私はあなたが正しく質問を理解している場合、私はあなたがwhereHas
例えばをしたいと思う私はlaravel 4.2
私はlaravel 5を使用していましたが、laravel 4.2 –
@CarlosSalazar 'whereHas'はまだ4.2で利用可能です。私は私の答えでリンクを更新しました。 –
IT WORKありがとう、最高です! –