How to process the resultset of a raw query in Laravel
I have a helper function which takes category as input and returns with all items for that category in comma separated string.
public static function getItemsByCategory($categoryId){
$categoryString="";
$query = DB::table('items');
$query->select(DB::raw('item_code'))
->where('category', '=', $categoryId)->get();
foreach($query as $result){
$categoryString= $categoryString.','.$result->item_code;
}
dd($categoryString);
return $categoryString; // output should be like "item1, item2, item3"
}
Also I am not being able to print out the item code using dd(). It says, "Undefined property: Illuminate\Database\MySqlConnection::$item_code"