skip to Main Content

Hello i have a table with many columns, each column has a row called phone_number now this row can either be null or filled with 1 or more phone numbers so its serialized the value. Now i want to fetch the phone numbers and this is the code i have until now

    foreach ($myActions as $action) {
        if ($action->phone_number) {
            $phoneArray = unserialize($action->phone_number);
        }
    }

I want to add a default value like —- so when i make this code:

<td>
@foreach($phoneArray as $phone)
    <p class="badge badge-info"> {{ $phone }} </p>
@endforeach
</td>

To show result:

1
*----*
2
3
4
*----*

2

Answers


  1. You don’t need to create any default value.
    You can use the PHP Null coalescing operator: ??, which will output the second expression if $phone is null

    <td>
     @foreach($phoneArray as $phone)
      <p class="badge badge-info"> {{ $phone ?? '*----*'}} </p>
     @endforeach
    </td>
    
    Login or Signup to reply.
  2. Check when variable phone is not empty

    <td>
    @foreach($phoneArray as $phone)
        <p class="badge badge-info"> {{ !empty($phone) ? $phone : "----" }} </p>
    @endforeach
    </td>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search