1

Hi everyone! The documents within my MongoDB collection can differ. Some have an extra embedded document called Additional_Information. I would like the web browser to display a table of the information within the Additional_Information when this is available, and to ignore it when it's not found! This should be the easiest if-statement, but I can't get it to work. I have tried this code:

@if (is_array($document['Additional_Information']))
      <li><a data-toggle="tab" href="#busco">Additional Information</a></li>
@endif

It does display the Additional Information tab when it is available. But when it is not, an Undefined index: Additional_Information it thrown! Does someone know how I can make this distinction between documents and catch when it's not there?

2
  • Is that code supposed to be PHP? Or does Mongo have it's own thing here? Commented Dec 1, 2016 at 13:13
  • 1
    @Machavity I think @if and @endif is Blade syntax (Laravel's template engine) Commented Dec 1, 2016 at 13:24

2 Answers 2

2

Use empty() instead:

@if (!empty($document['Additional_Information']))
      <li><a data-toggle="tab" href="#busco">Additional Information</a></li>
@endif

According to the documentation:

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

Sign up to request clarification or add additional context in comments.

1 Comment

I knew it has to be something simple! Thank you!
0

you need to use array_key_exists as well in order to check if key Additional_Information exists or not in the array $document

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.