0

I have an issue using @if, @elseif and @else statement on blade.php using laravel 8, It always return as Undefined Variable. How can I resolve this problem? Or is there any other away around?

On my index.blade.php:

@if($exi)
@include('renew.modify.exi')
@elseif($exi2)
@include('renew.modify.exi2')
@elseif($exp)
@include('renew.modify.exp')
@endif

On my modifyController.php:

if (count((array) $exi) > 0) {
  return View::make('renew.modify.index', ['exi' => $exi]);
}
elseif(count((array) $exi2) > 0) {
  return View::make('renew.modify.index', ['exi2' => $exi2]);
}
elseif(count((array) $exp) > 0) {
  return View::make('renew.modify.index', ['exp' => $exp]);
} else {
  Session::flash('flash_message', 'Something went wrong on your credentials. Please double check your inputs. If you don\'t have any. You can create here: ');
  return Redirect::to('renewView');
}

My goal is, when the controller detect that count((array) $something) detect then it will return to the same index.blade then the @if, @elseif and @else will be triggered, but when $exp detected the my two variable will be undefined. How do i resolve this problem?

Syfy
  • 55
  • 7
  • Pass the other two variables to each view, even if the value may be 0, so that all 3 are defined. Only one of the 3 should evaluate true, and all variables will be present to the blade. That is at least one way. – Paul T. Jul 28 '22 at 02:24
  • I was using the same method on laravel 5 and its working, but now using laravel 8 it starts to make it undefined. – Syfy Jul 28 '22 at 02:33
  • Ok, but that was 3 versions ago? Apparently, some were [reporting this back with Laravel 5](https://stackoverflow.com/questions/18497788/laravel-breaks-entire-app-on-php-notices) (and 4), so there must be configuration differences? I don't know that I would try to subvert notices, but know that with PHP 8+, undefined vars are now warnings, not notices. – Paul T. Jul 28 '22 at 03:04

1 Answers1

0

You can use isset() method for that. change your @if condition like this way

@if(isset($exi))
@include('renew.modify.exi')
@elseif(isset($exi2))
@include('renew.modify.exi2')
@elseif(isset($exp))
@include('renew.modify.exp')
@endif

may it helps ...

  • Yep, I should read the document again, I literally forgot about the `isset`. Thank you very much! – Syfy Jul 28 '22 at 04:47