0

I do have a table with data like this

Product

id     |    name   |     day    |    time
1      |    prod1  |     MON    |    08:30
2      |    prod1  |     MON    |    09:30
3      |    prod1  |     TUE    |    10:30
4      |    prod2  |     WED    |    05:30
5      |    prod3  |     MON    |    11:30

how do I return it in POSTMAN like this:

prod1 {
     "MON" : [
          08:30,
          09:30
      ],
     "TUE" : [
          10:30
      ]
}
2
  • take a look at this: laravel.com/docs/5.5/eloquent-resources Commented Jan 18, 2018 at 16:14
  • 1
    You need to manipulate the Product data in the Controller that's handling this request, then send it back using a standard response, like response()->json(["data" => $data]);, etc. That being said, what's the error you're getting? Have you actually tried this? Please include your code. Commented Jan 18, 2018 at 16:16

1 Answer 1

3

try this one:

$products = Product::all();
$jsonOutput = [];

foreach($products as $product){
   $jsonOutput[$product['day']][] = $product['time'];
}

return response()->json($jsonOutput);
Sign up to request clarification or add additional context in comments.

1 Comment

Can I do that using this? $time_date = DB::table('product_time'>select('product_time')->where('product_id', '=', $product_id)->get();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.