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>
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
Try to use $loop->last()
:
@foreach($product->Offers as $offer)
{
"@type": "Offer",
"name": "{{$offer->title}}",
"price": "{{$offer->displayPrice}}",
}{{ $loop->last() ? '' : ',' }}
@endforeach