Search code examples
laravelphp-7

shorthand isset followed by eval


Setting up some nav links in laravel and trying to setup some inline checks based on the URL hit which will make the nav link "active" or not, but running into an issue where i can't run an isset() before the expression. Not sure where I am falling short!

Just FYI - $third is the third bit of the URL - in this case the url is only at the $second level so $third is not defined. which means I don't want the statement to run.

<a class="nav-link {{ $third ?? $third === 'introduction' ? 'active' : '' }}" href="xyz"

How do i run

$third === 'introduction' ? 'active' : ''

only when $third exists!

Thanks


Solution

  • Shouldn't this work?

    <a class="nav-link {{ isset($third) ? ($third === 'introduction' ? 'active' : '') : '' }}" href="xyz">