I am getting the following error when loading a page:
Undefined variable: userIds
However, the variavle is reaching the partial given when I try {{ isset($userIds) ? 'Yes' : 'No' }}
it returns ‘Yes’ when the block of code is reached.
The error occurs with the variable $userIds in the following input type:
</li> @foreach ($users as $index => $user) <li class="list-group-item" style="{{ (isset($usersFiltered) && is_array($usersFiltered) && !in_array($user->id, $usersFiltered) ) ? 'display: none;' : ''}}"> <div class="checkbox"> <label> <input type="checkbox" class="awe-selectable" id="users[{{ $index }}]" name="users[{{ $index }}]" value="{{ $user->id }}" {{ in_array($user->id,$userIds) ? 'checked' : '' }}> <label for="users[{{ $index }}]">{{ $user->name }}</label> </label> </div> </li> @endforeach
My controller code sending the variable to the partial:
return view('core::admin.groups.partials._users_form', [ 'group' => $group, 'users' => $users, 'usersFiltered' => $usersFiltered, 'userIds' => $userIds, ]);
Is it a syntax mistake? If I replace $userIds by any of the other variables, those too will return the same “undefined” error (f.e. with $usersFiltered)
Thanks in advance, will edit the post with more information as needed.
Advertisement
Answer
Solved!
The issue was that when the page was rendered, the variable was not defined in the main page render (/create), only in the method that rendered the partial, so when I was opening the main window, the variable was not yet defined because it was only received from the controller once a filter was applied.
Sollution:
/** * GET: /admin/security/groups/create */ public function create() { **$userIds = !empty($request['userIds']) ? $request['userIds'] : [];**
(…)
return view('core::admin.groups.create', [ 'group' => new Group, **'userIds' => $userIds,** 'users' => $users, 'isCreating' => true, ]);