How to remove the space before a comma in laravel blade view?

Viewed 1210
@foreach($opt as $key => $o)
   @if($o == 1)A @endif
   @if($o == 2)B @endif
   @if($key+1 != count($opt)), @endif
@endforeach

How can i show the expected output A, B, C ?

//Current Output       A , B , C        
//Expected Output      A, B, C
2 Answers

You could do something like this:

@foreach($opt as $key => $o)
    {{ $o == 1 ? 'A' : '' }}{{ $o == 2 ? 'B' : '' }}@if($key+1 != count($opt)), @endif
@endforeach

I find it a total pain to keep the blade syntax readable, so I duplicate the word prior to the comma in the if/else statement.

Desired output:

<p>Some text goes here, with this conditional text</p>

Blade structure

<p>Some text goes
    @if($condition)
        here, with this conditional text
    @else
        here
    @endif
</p>
Related