'How to add html in shorthand if with blade tags

How do i add a span tag around the $product->category->name in one line?

       <td>{{$product->category ? $product->category->name : 'category not known'}}</td>
      <span class="badge bg-danger">{{$product->category->name}}</span><br>


Solution 1:[1]

In your comment you have a blade echo in your blade echo, just remove that one and add '( ... )':

<td>{!! $product->category ?  ('<span class="badge bg-danger">' . $product->category->name . '</span>') : 'category not known'!!}</td>

Solution 2:[2]

Are You looking for this shorthand?

<span class="badge bg-danger">{{$product->category->name ?? 'category not known'}}</span><br>

This is called the "null coalescing operator"

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gert B.
Solution 2 Ma-saddqiui