Q:

Generate object array with values from Database

Suppose I have this table:
id    |    product_item    |    head_count
1     |         400        |        2
2     |         401        |        5

desired output:
[
   {"product_item": 400,"head_count": 2},
   {"product_item": 401,"head_count": 5}
]

How do I make this desired output using Laravel or Eloquent? 

A:

Follow bellow step  
(1) declare in your controller **use DB**  

(2) write Query:  
$product = DB::table('tablename')->select('col1','col2')->get(); 

(3)dd($product); OR print_r($product);  

You got the result in $product  

echo json_encode($product); 

OR if you are load your view then,  
return view('VIEW_NAME',compact('product'));  

OR return like this and got the result on your view file.

return response()->json($product);

I hope its help.

