I new on laravel. So i tried to save this data to my Database and display it on my app. I managed to save it but i can't display the data.
{
"2": {
"name": "Ice Latte",
"quantity": 1,
"price": 20000
},
"3": {
"name": "Ice Lemon Tea",
"quantity": 1,
"price": 20000
},
"6": {
"name": "Ice Chocolate",
"quantity": 1,
"price": 20000
}
}
Here is my Order table migrations
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->string('name')->default('guest');
$table->json('data');
$table->boolean('isActive')->default(1);
$table->timestamps();
});
Here is my Order table model
protected $fillable = ['name','data','isActive'];
protected $casts = [
'data' => 'array'
];
I've tried {{ $order->data[0] }}
on my view. Where i tried to display the data.
but it said undefined array key 0
.
So i check it with {{print_r(array_keys($order->data))}}
and this is what it returns Array ( [0] => 2 [1] => 6 [2] => 3 ) 1
I want the data to be display like this
Ice Latte (2)
Ice Lemon Tea (1)
Ice Chocolate (1)
CodePudding user response:
you need to define index of $order as:
{{ $order[0]->data; }}
CodePudding user response:
use loop on your view.
@foreach($orders as $order)
{{ $order->name }}
...
// other order keys
@endforeach