0

This is my query:

$items = UserItems::with('item')
            ->where('user_id','=',$this->id)
            ->where('quantity','>',0)
            ->where('items.type','=',"shirt")
            ->get();

I need all items where type is shirt.

The query returns this:

Column not found: 1054 Unknown column 'items.type' in 'where clause'

For some reasons, the items is not recognized as a table in this query, and I can't use `where on it.

Then how I can get all user items where the item type is shirt?

TheUnreal
  • 23,434
  • 46
  • 157
  • 277
  • is your table called 'item' or 'items'? if so, change -> with('item') to with('items'), if not, change -> ->where('items.type','=',"shirt") to ->where('item.type','=',"shirt") – Carlos May 20 '16 at 20:37
  • I tried both, it didn't work – TheUnreal May 21 '16 at 07:07

2 Answers2

1

Decision

Try:

$items = UserItems::with(['item' => function($query){
    return $query->where("type", "shirt")
})
->where('user_id','=',$this->id)
->where('quantity','>',0)
->get();
Community
  • 1
  • 1
Andrii Lutskevych
  • 1,349
  • 13
  • 23
1

You will actually need to use whereHas to handle this...

$items = UserItems::with('item')
        ->whereHas('item', function($q) {
            $q->where('type', 'shirt');
         })
        ->where('user_id','=',$this->id)
        ->where('quantity','>',0)
        ->get();
user1669496
  • 32,176
  • 9
  • 73
  • 65