Hi I am trying to check the variable is already set or not using blade version. But the raw php is working but the blade version is not. Any help?
controller:
public function viewRegistrationForm() { $usersType = UsersType::all(); return View::make('search')->with('usersType',$usersType); }
view:
{{ $usersType or '' }}
it shows the error :
Undefined variable: usersType (View: C:\xampp\htdocs\clubhub\app\views\search.blade.php)
The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
Assumption #1: You Know The Variable Exists Within The View. REMEMBER: an empty array will always return false. Therefore, there is no real need to run it through a function like empty or is null. Comparing it to null will tell you if it exists or not.
The Blade is a powerful templating engine in a Laravel framework. The blade allows to use the templating engine easily, and it makes the syntax writing very simple. The blade templating engine provides its own structure such as conditional statements and loops.
{{ $usersType or '' }}
is working fine. The problem here is your foreach loop:
@foreach( $usersType as $type ) <input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span> @endforeach
I suggest you put this in an @if()
:
@if(isset($usersType)) @foreach( $usersType as $type ) <input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span> @endforeach @endif
You can also use @forelse. Simple and easy.
@forelse ($users as $user) <li>{{ $user->name }}</li> @empty <p>No users</p> @endforelse
@isset($usersType) // $usersType is defined and is not null... @endisset
For a detailed explanation refer documentation:
In addition to the conditional directives already discussed, the
@isset
and@empty
directives may be used as convenient shortcuts for their respective PHP functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With