简体   繁体   中英

Undefined variable in laravel blade partial view error

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' }} {{ 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' : '' }}>
                    &nbsp;&nbsp;
                    <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 (fe with $usersFiltered)

Thanks in advance, will edit the post with more information as needed.

the syntax looks good to me, but I would suggest to you to do this:

// in your controller
foreach ($users as $user) {
 $user->checked = false;
 if ((is_array($userIds) and !empty($userIds)) and in_array($user->id, $userIds)) {
     $user->checked = true;
 }
}

now there is no need to send the $userIds to the view, since you fixed it in the controller.

// in your view
<input type="checkbox" class="awe-selectable" id="users[{{ $index }}]" name="users[{{ $index }}]" value="{{ $user->id }}" {{  ($user->checked) ? 'checked' : '' }}>

Happy coding!

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,
        ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM