Tuesday, February 28, 2017

Returning Laravel DB::table('...')->get() as a Collection of Model instead of StdClass

The DB::table('my_table')->get() returns a Collection of StdClass.

Is it possible to return a collection of MyTable, instead of StdClass? Is there a "laravel way" of doing this?

To ilustrate my problem, I have this query on my model Item:

        return DB::table('attribute_values')
          ->join('items_categories', 'attribute_values.item_category_id', '=', 'items_categories.id')
          ->where('items_categories.item_id', '=', $this->id)
          ->select('attribute_values.*')
          ->get();

and I need the collection of model AttributeValue. Currently I'm iterating through the collection of stdClass and instantiating the AttributeValue's.




via Victor

Advertisement