Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cmellor-enigma's avatar

"Undefined variable $errors" When Testing A View

Hello,

I am looking at testing a view of mine, just to see if the text I expect to see is there.

$view = $this->view(view: 'admin.users.create', data: [
    'roles' => \App\Models\Role::all(),
]);

$view->assertSee(value: ucfirst(string: \App\Enums\RoleName::SuperAdmin->value));

But I am getting an error about an Undefined variable $errors

Here's the whole error form the CLI

  • Tests\Feature\Http\Controllers\Admin\UserControllerTest\CreateTest > a "Super Admin" can see all roles
   Illuminate\View\ViewException

  Undefined variable $errors (View: /Users/chris.mellor/Dev/59club-platform/resources/views/admin/users/create.blade.php)

  at storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
     14▕         </div>
     15▕
     16▕         <div class="errors">
     17▕             <ul>
  ➜  18▕                 <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
     19▕                     <li><?php echo e($error); ?></li>
     20▕                 <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
     21▕             </ul>
     22▕         </div>

      +1 vendor frames
  2   storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
      ErrorException::("Undefined variable $errors")

  3   storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
      Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}("Undefined variable $errors", "/Users/chris.mellor/Dev/59club-platform/storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php")

Yes, the view in question does use the $errors variable as it is checking for validation as the view has a form.

Thought this would be a simple one, but I feel I may have missed something?

Any help is appreciated.

Chris

0 likes
12 replies
Sinnbeck's avatar

Can you just try

$view = $this->view(view: 'admin.users.create', data: [
    'roles' => \App\Models\Role::all(),
     'errors' => [],
]);
cmellor-enigma's avatar

@Sinnbeck gave that a try, and it gives me this:

  • Tests\Feature\Http\Controllers\Admin\UserControllerTest\CreateTest > a "Super Admin" can see all roles
   Illuminate\View\ViewException

  Call to a member function all() on array (View: /Users/chris.mellor/Dev/59club-platform/resources/views/admin/users/create.blade.php)

  at storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
     14▕         </div>
     15▕
     16▕         <div class="errors">
     17▕             <ul>
  ➜  18▕                 <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
     19▕                     <li><?php echo e($error); ?></li>
     20▕                 <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
     21▕             </ul>
     22▕         </div>

      +1 vendor frames
  2   storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
      Error::("Call to a member function all() on array")

      +7 vendor frames
  10  tests/Feature/Http/Controllers/Admin/UserControllerTest/CreateTest.php:44
      Illuminate\Foundation\Testing\TestCase::view("admin.users.create", [Object(Illuminate\Database\Eloquent\Collection)])


  Tests:  1 failed, 1 skipped, 14 passed
  Time:   3.41s

Which seems to be related to the

<div class="errors">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>

code in the create.blade.php file. I'm struggling to think how to even debug it. Works fine manually :\

tykus's avatar

@cmellor-enigma I wonder what value your test actually provides; you send all of the Roles to the view; and you assert that you see Super Admin? How does that prove the functionality that a "Super Admin" can see all roles?

cmellor-enigma's avatar

@tykus When I view the create page, logged in as a User with the role super-admin I just want to assert that it can see those roles, or at least one in the list with the same value.

I'm new-ish to Testing, so I'm just learning as I go. Spotted I could tests Views like this and figured it seemed like a viable test 🤷🏻‍♂️

tykus's avatar

@cmellor-enigma if you're only testing the view; there is no logged in User; this needs to be a full Feature test with a Request.

I don't see any value in this test as it is implemented.

cmellor-enigma's avatar

@tykus I didn't specify this, or give the whole code.

I am logging in as a User in the test

$user = \App\Models\User::factory()->createOne()
    ->attachRole(\App\Enums\RoleName::SuperAdmin->value);

loginAs(user: $user);

Without this I get 403.

tykus's avatar

@cmellor-enigma you get a 403 because you have authorization checks in the view?

MohamedTammam's avatar

I believe that test should be in a feature tests as mentioned by @tykus.

And, did you try what @sr57 suggested, what was the result.

$this->withViewErrors([])->view(view: 'admin.users.create', data: [
    'roles' => \App\Models\Role::all(),
]);
1 like
cmellor-enigma's avatar

@MohamedTammam I seem to have skipped over it in a rush 🤦🏻‍♂️

Sorry @sr57 for the oversight, that does indeed give me the solution I want :)

1 like
tykus's avatar

@cmellor-enigma I'd still question the value of that test... what does it actually prove that would not be better handled by a feature test - you are bypassing the Controller logic that returns that view.

1 like
cmellor-enigma's avatar

@tykus I'm still new to Testing, trying to learn as much as I can by doing. This may well be something that is irrelevant, but I can learn from it.

Thanks.

Please or to participate in this conversation.