2016-10-21 47 views
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

答えて

1

を使用しています言及するのを忘れて

$items = Item::with('imagenes') 
     ->whereHas('tallas', function ($query) use ($dataFilter) { 
      $query->where('id', '=', $dataFilter['filter-size']); 
     }); 

https://laravel.com/docs/4.2/eloquent#querying-relations

この情報がお役に立てば幸い!

+0

私はlaravel 5を使用していましたが、laravel 4.2 –

+0

@CarlosSalazar 'whereHas'はまだ4.2で利用可能です。私は私の答えでリンクを更新しました。 –

+0

IT WORKありがとう、最高です! –

関連する問題