Search code examples
laraveleloquentlaravel-8laravel-7laravel-6

Hyphen in between categories name but when i use foreach for repeat name


i use laravel 8 and i want to use of this Hyphen - in between categories name but when i use foreach for repeat name. this Hyphen - is repeat in final category name.

   @foreach($category_tags as $category_tag)
    <a href="{{$category_tag->link}}">{{$category_tag->title}} </a> -
    @endforeach

my controller code

    $category_tags = ChildCategory::where('parent', $category->id)->where('status', 1)->get();

category: book - shop - phone - test - tablet -

i want remove this Hyphen- after tablet - and see

category: book - shop - phone - test - tablet


Solution

  • Laravel provides helper object inside foreach one of them called $loop->last this will help you in this case

    @foreach($category_tags as $category_tag)
        <a href="{{$category_tag->link}}">{{$category_tag->title}} </a> 
        @if(!$loop->last) 
        -
        @endif
    @endforeach
    

    for more info check the docs