Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blade if(isset) is not working Laravel

Tags:

php

laravel

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)

like image 734
WahidSherief Avatar asked Feb 13 '15 10:02

WahidSherief


People also ask

What is Isset in laravel?

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.

How do you check if a variable is empty in laravel?

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.

What is Blade syntax in laravel?

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.


2 Answers

{{ $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> &nbsp;  @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> &nbsp;      @endforeach @endif 

You can also use @forelse. Simple and easy.

@forelse ($users as $user)    <li>{{ $user->name }}</li> @empty    <p>No users</p> @endforelse 
like image 197
lukasgeiter Avatar answered Sep 19 '22 01:09

lukasgeiter


@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

like image 34
Bhargav Kaklotara Avatar answered Sep 19 '22 01:09

Bhargav Kaklotara