How to Check Array in Blade View Template

Posted on

This is an article where it is actually written to check an array in blade view template file. So, how to check array in a blade view template actually ?. It can be done by using a syntax for counting the actual size of the array variable itself. The actual scenario is a variable generated in a controller named UserController which exists in a file named ‘UserController.php’. The variable itself is passed to a blade view template file where the name of the file for the example in this context is edit.blade.php. Below is the content of each files :

UserController.php :

$all_role = DB::table('role')->get()->toArray();

$available_role = DB::table('users')
->leftJoin('user_role','users.id','=','user_role.id_user')
->leftJoin('role','role.id_role','=','user_role.id_role')
->where('users.id','=',$id_user)
->where('user_role.id_role','<>',null)
->get();

$all_server = DB::table('server')->get()->toArray();

$data = array("user" => $selected_user, "all_role" => $all_role, "avail_role" => $available_role, "all_server" => $all_server);

return view('user.edit')->with('data', $data);
}

The blade view template named ‘edit.blade.php’ :

<div class="col-md-12" id="div-form-header">
<div class="col-md-4 col-md-offset-2" id="div-header">
<label class="col-md-4 control-label">Role Name</label>
<label class="col-md-4 control-label">Server Name</label>
<label class="col-md-4 control-label">Action</label>
</div>
@if(count($data['avail_role']) == 0)
<div class="col-md-12 col-md-offset-2" id="div-content">
<label class="col-md-3 control-label" id="label-no-role">There is no role</label>
</div>
@else else
@foreach($data['avail_role'] as $role)
<label class="col-md-3 control-label">{{$role->level}}</label>
<label class="col-md-3 control-label">Server Location</label>
<input class="col-md-2" id="btn-{{$role->level}}" type="button" value="Delete" />
@endforeach
@endif
</div>

As it can be seen in the above snippet code of a blade view template file named edit.blade.php there is a specific code used to measure the size of the array variable printed in the blade view template file. Below is that specific code :

@if(count($data['avail_role']) == 0)

The function shown in the above snippet code or specific code ‘count’ is used to count the size of the array variable passed from the controller file to the blade view template file.

The scenario in the above two files presented can be described in the following narration as follows :

The available role is going to be printed out in the form of query where the result is passed into a variable :

$available_role = DB::table('users') ->leftJoin('user_role','users.id','=','user_role.id_user') ->leftJoin('role','role.id_role','=','user_role.id_role') ->where('users.id','=',$id_user) ->where('user_role.id_role','<>',null) ->get();

So, to be able to see what kind of role are available or granted to a user, the above query is executed. After retrieving the available role for user specified with the id of ‘$id_user’ stored into the variable named ‘$available_role’ where it is finally compiled into one variable named ‘data’ as shown below :

$data = array("user" => $selected_user, "all_role" => $all_role, "avail_role" => $available_role, "all_server" => $all_server);

And after that, the $data variable will be passed into a blade view template file named ‘edit.blade.php’ as shown in the following snippet code :

return view('user.edit')->with('data', $data);

The variable named ‘data’ itself later on it is iterated in the blade view template file. But before the variable itself is being iterated, first of all it must be checked to see whether it has an element inside of it or not. If it doesn’t have any element inside the variable, then just print the label informing that the user edited doesn’t have any role as shown below :

<div class="col-md-12 col-md-offset-2" id="div-content">
<label class="col-md-3 control-label" id="label-no-role">There is no role</label>
</div>

 

 

 

One thought on “How to Check Array in Blade View Template

Leave a Reply