I need to know what the last item in the vector is so that when the last one does not add a comma:
Example:
SEM SABER QUAL É O ULTIMO ITEM
1,2,3,4,5,
SABENDO QUAL É A ULTIMA VIRGULA
1,2,3,4,5
I make a foreach
in the variable, and increment a comma to the text, however in the last one it will not be necessary to add the comma because it is to the last one. I'm using LARAVEL 5.4
along with blade
for HTML
manipulation.
Follow the code:
@foreach($cliente->compras as $compra)
{{$compra->produto}},
@endforeach
The way it is above, it is putting the last comma, but it is normal taking into account that it is not doing any logic to leave it.
Relationships and Model
Shopping
public function cliente()
{
return $this->BelongsTo('App\Compras', 'COD_IDENT_CLIEN', 'COD_COMPR_VENDA');
}
public function produtos()
{
return $this->HasMany('App\ComprasProdutos', 'COD_DAXXX_VENDA', 'COD_DAXXX_VENDA');
}
Shopping - > Products
public function compra()
{
return $this->BelongsTo('App\Compras', 'COD_DAXXX_VENDA', 'COD_DAXXX_VENDA');
}
public function item()
{
return $this->BelongsTo('App\Produto', 'COD_IDENT_PRODU', 'COD_BARRA_PRODU');
}
Products
public function compras()
{
return $this->BelongsTo('App\ComprasProdutos', 'COD_BARRA_PRODU', 'COD_IDENT_PRODU');
}
Code Blade
@foreach($cliente->compras as $compra)
<div class="list-group-item media">
<div class="checkbox pull-left">
#{{$compra->COD_DAXXX_VENDA}}
</div>
<div class="pull-right">
<div class="actions dropdown">
<a href="" data-toggle="dropdown" aria-expanded="true">
<i class="zmdi zmdi-more-vert"></i>
</a>
<ul class="dropdown-menu dropdown-menu-right">
<li>
<a href="/clientes/compra/{{ $cliente->COD_IDENT_CLIEN }}">
Visualizar
</a>
</li>
</ul>
</div>
</div>
<div class="media-body">
<div class="lgi-heading">
@foreach($compra->produtos as $produto)
{{-- {{dd($produto->item)}} --}}
{{$produto->item->TXT_NOMEX_PRODU}}
@if ($produto->last != $produto)
{{','}}
@endif
@endforeach
</div>
</div>
</div>
@endforeach
</div>