0

The database table looks like this:

enter image description here

The "extras" column in the database is storing an array of objects that look like this:

[
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"},

{"id":93,"product_id":8966,"extra_type":"Extras","extra_name":"Ketchup"},

{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"}
]

The code below (from the Laravel blade) returns the array of objects.

@foreach($item->orderedProducts as $op)
<tr>
     <td>
         <span>{{ $op['extras'] }}</span><br>
     </td>
</tr>
@endforeach

$op returns this:

{
  "id": 171,
  "product_id": 8966,
  "order_id": 175,
  "price": 11,
  "count": 1,
  "product_data": "{\"id\":8966,\"name\":\"Camera Br\\u00fbl\\u00e9e\",\"description\":\"Lorem ipsum dolor sit amet consectetur adipiscing elit etiam, conubia tempus sed dapibus augue gravida accumsan. Odio congue in blandit iaculis risus gravida parturient dictum quis rhoncus volutpat ornare tincidunt, dignissim ut pellentesque.\",\"price\":11,\"price_old\":null,\"category_id\":4584,\"created_at\":\"2019-03-17 14:59:56\",\"updated_at\":\"2019-04-17 16:37:03\",\"tax_group_id\":null,\"sort\":1,\"vendor_id\":null,\"option1\":null,\"option2\":null,\"option3\":null,\"option4\":null,\"option5\":null,\"option6\":null,\"images\":[\"http:\\/\\/localhost:8000\\/product_images\\/ElI9GImttc.jpg\"],\"formatted_price\":\"\\u00a311\",\"formatted_old_price\":\"0\",\"tax_value\":0,\"city_id\":null,\"restaurant_id\":null,\"product_images\":[{\"id\":9000,\"image\":\"\\/product_images\\/ElI9GImttc.jpg\",\"product_id\":8966,\"created_at\":\"2019-04-08 15:16:53\",\"updated_at\":\"2019-04-08 15:16:53\"}],\"tax_group\":null,\"category\":{\"id\":4584,\"name\":\"Random Things\",\"_lft\":1,\"_rgt\":2,\"parent_id\":null,\"created_at\":\"2018-11-09 13:15:01\",\"updated_at\":\"2019-04-17 16:47:21\",\"restaurant_id\":null,\"city_id\":null,\"category_image\":null,\"has_children\":0,\"image_url\":\"http:\\/\\/localhost:8000\\/category_images\\/a64be5a696402b0fe3649536ab6a49e4_1555519641.jpg\"},\"added\":true}",
  "extras": "[{\"id\":93,\"product_id\":8966,\"extra_type\":\"Extras\",\"extra_name\":\"Ketchup\",\"extra_price\":\"1.20\",\"extra_added\":true,\"price_sum\":1.2,\"extra_count\":1}]",
  "exclusions": "[{\"id\":117,\"product_id\":8966,\"extra_type\":\"Exclusions\",\"extra_name\":\"Rat poison\",\"extra_price\":null,\"exclusion_added\":true}]",
  "created_at": "2019-04-27 10:35:11",
  "updated_at": "2019-04-27 10:35:11",
  "product": {
    "id": 8966,
    "name": "Camera Brûlée",
    "description": "Lorem ipsum dolor sit amet consectetur adipiscing elit etiam, conubia tempus sed dapibus augue gravida accumsan. Odio congue in blandit iaculis risus gravida parturient dictum quis rhoncus volutpat ornare tincidunt, dignissim ut pellentesque.",
    "price": 11,
    "price_old": null,
    "category_id": 4584,
    "created_at": "2019-03-17 14:59:56",
    "updated_at": "2019-04-17 16:37:03",
    "tax_group_id": null,
    "sort": 1,
    "vendor_id": null,
    "option1": null,
    "option2": null,
    "option3": null,
    "option4": null,
    "option5": null,
    "option6": null,
    "images": [
      "http://localhost:8000/product_images/ElI9GImttc.jpg"
    ],
    "formatted_price": "£11",
    "formatted_old_price": "0",
    "tax_value": 0,
    "city_id": null,
    "restaurant_id": null,
    "product_images": [
      {
        "id": 9000,
        "image": "/product_images/ElI9GImttc.jpg",
        "product_id": 8966,
        "created_at": "2019-04-08 15:16:53",
        "updated_at": "2019-04-08 15:16:53"
      }
    ],
    "tax_group": null,
    "category": {
      "id": 4584,
      "name": "Random Things",
      "_lft": 1,
      "_rgt": 2,
      "parent_id": null,
      "created_at": "2018-11-09 13:15:01",
      "updated_at": "2019-04-17 16:47:21",
      "restaurant_id": null,
      "city_id": null,
      "category_image": null,
      "has_children": 0,
      "image_url": "http://localhost:8000/category_images/a64be5a696402b0fe3649536ab6a49e4_1555519641.jpg"
    }
  }
}

What I am trying to achieve is obtain all the values of (for example) "extra_name" from "extras:".

The desired output should be something like:

Olives Ketchup Olives

2
  • Can you try {{ $op->extras }}? Commented Apr 27, 2019 at 10:25
  • You need {{ $op->extra_type }}, I guess. Commented Apr 27, 2019 at 10:33

4 Answers 4

1
@foreach($item->orderedProducts as $op)
<tr>
    <td>
    @if ('Extras' == $op->extra_type)
         <span>{{ $op->extra_name }}</span><br>
    @endif
    </td>
</tr>
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

1

I think this will workout

@foreach($item->orderedProducts as $op)
<?php $array = stripslashes(json_encode($op->extras)) ?>
    <tr>
         <td>
             <span>
             @foreach($array as $value)
                 {{ $value['extra_name'] }}
             @endforeach
             </span><br>
         </td>
    </tr>
 @endforeach

5 Comments

ErrorException: Invalid argument supplied for foreach()
Can you please post what $op prints...by doing=> print_r($op) inside the first foreach
or you can try replacing $op['extras'] by $op->extras in 2nd foreach
I have added what $op returns. Do you know why there are ' \ ' in the json?
ok, it is taken as string which is a json. Anyway you have to format "extras" before 2nd foreach to json format. then you are all done. cheers
0

You can access objects value by using -> in laravel, try this

@foreach($item->orderedProducts as $op)
<tr>
 <td>
     <span>{{ $op->extra_name }}</span><br>
 </td>
</tr>
@endforeach

Comments

0

I solved it. What I needed was to use json_decode() while iterating through an array of json objects. Next, I extracted the attributes I needed.

@foreach($item->orderedProducts as $op)
<tr>
    <td>
        @foreach(json_decode($op->extras, true) as $extra)
             {{ $extra['extra_name'] }}<br>
        @endforeach
    </td>
</tr>
@endforeach

Comments

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.