John
John

Reputation: 207

How to echo html tag in blade of Laravel

I want to echo button to my blade but I don't know how to do. So, how to do that? Thanks you.

<td class="nowrap">{!! $order->status=='0'?
    @php
    echo '<button class="btn btn-danger">Inactive</button>';
    @endphp:
    @php
    echo '<button class="btn btn-success">Active</button>';
    @endphp
    !!}
</td>

Upvotes: 1

Views: 11964

Answers (4)

AndyPHP
AndyPHP

Reputation: 381

You can put HTML code in PHP variable as a string and that variable should be placed in {!! !!} braces.

Example:

{!! $text !!}

Upvotes: 4

Renish Khunt
Renish Khunt

Reputation: 5824

You can do that using two ways first use if condition like below

<td class="nowrap">
@if( $order->status == '0' )
    <button class="btn btn-danger">Inactive</button>
@else
    <button class="btn btn-success">Active</button>
@endif
</td>

The second and proper way for use ternary operator on blade

<td class="nowrap">
      {!! $order->status=='0' ? '<button class="btn btn-danger">Inactive</button>' : '<button class="btn btn-success">Active</button>' !!}
</td>

I hope the second way is perfect for used ternary operator on blade.

Upvotes: 5

Poiz
Poiz

Reputation: 7617

In blade you could do something like this:

<td class="nowrap">
    @if($order->status=='0')
        <button class="btn btn-danger">Inactive</button>';
    @else
        <button class="btn btn-success">Active</button>';
    @endif 
</td>

Upvotes: 2

Autista_z
Autista_z

Reputation: 2541

<td class="nowrap">
   @if($order->status=='0')
      <button class="btn btn-danger">Inactive</button>
   @else
      <button class="btn btn-success">Active</button>
   @endif
</td>

or

<td class="nowrap">
   <button class="btn btn-{{($order->status=='0') ? 'danger' : 'success'}}">{{($order->status=='0') ? 'Inactive' : 'Active'}}</button>
</td>

Upvotes: 2

Related Questions