Back

Display multiple json objects present in the single data cell


I have a column say placeditems where each time an user places his order a json object is stored.So, i'm able to display the first json object(first order) successfully.It throws an as Invalid argument supplied for foreach() for the second and forth objects .. So i want to add new order in the existing object.

Collection {#207 ▼
  #items: array:1 [▼
    0 => Order {#211 ▼
      #casts: array:1 [▶]
      #fillable: array:8 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:10 [▼
        "id" => 1
        "tableno" => "9"
        "noofitems" => 2
        "placeditems" => ""[{\"item\":\" Cafe Latte\",\"qty\":\"2\"}][{\"item\":\" Egg Sandwich\",\"qty\":\"3\"}]""
        "total" => 106
        "paidstatus" => 0
        "deliveredstatus" => 0
        "created_at" => "2017-07-14 11:32:29"
        "updated_at" => "2017-07-14 11:36:37"
        "username" => "xyz"
      ]
      #original: array:10 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

my model Order

/**
   * The attributes that should be cast to native types.
   *
   * @var array
   */
protected $casts = [
    'placeditems' => 'array',
];

VIEW

@foreach($orders as $order)
      @foreach(json_decode($order->placeditems, true) as $item)
      <p>
          {{ $item['item'] }}
          <span>
          ( {{ $item['qty'] }} )
          </span>
      </p>
      @endforeach
    @endforeach

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us