Reputation: 3418
I tried adding last item logic using if condition, but the code is getting displayed.
My existing code :
<script type='application/ld+json'>
{
"itemListElement":
[
@foreach($product->Offers as $offer)
{
"@type": "Offer",
"name": "{{$offer->title}}",
"price": "{{$offer->displayPrice}}",
},
@endforeach
]
}
}
</script>
Upvotes: 0
Views: 1071
Reputation: 2736
Try to do this
<?php $i = 1; $len = count($product->Offers); ?>
@foreach($product->Offers as $offer)
{
"@type": "Offer",
"name": "{{$offer->title}}",
"price": "{{$offer->displayPrice}}",
}<?php if($i < $len){echo ',';} $i++;?>
@endforeach
Upvotes: 1
Reputation: 163968
Try to use $loop->last()
:
@foreach($product->Offers as $offer)
{
"@type": "Offer",
"name": "{{$offer->title}}",
"price": "{{$offer->displayPrice}}",
}{{ $loop->last() ? '' : ',' }}
@endforeach
Upvotes: 4