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

Raed AlGherbawi's avatar

please, I want a solution to this problem,

ErrorException Undefined variable: users (View: C:\wamp64\www\fastclick\resources\views\components\users.blade.php)

This problem is bothering me and I can't find a solution for it

Project: laravel 8

Controller Code

namespace App\Http\Controllers use Illuminate\Http\Request use Illuminate\Support\Facades\DB class Users extends Controller { public function getAllUser() { $users = DB::table('users')->get(); return view('pages/users', compact('users')); } }

Route

Route::get('users',[Users::class,'getAllUser'])->name('all.user');

View Code

@foreach($users as $key => $user)

    <li>{{$user->id}}</li>

    <h1>{{$user->name}}</h1>

    <h2>{{$user->body}}</h2>

@endforeach
This data and the code does not work, knowing that I work in programming in the way of calling pages

I tried to modify the display code in several ways, knowing that the table in the database is called users

0 likes
32 replies
tykus's avatar

The exception message relates to resources\views\components\users.blade.php, whereas the Controller action returns resources/views/pages/users - so you are doing something else here with the component???

Raed AlGherbawi's avatar

this source code to main page

The page is located in a folder called pages
tykus's avatar

@rado82r@gmail.com the error message says that the undefined variable is in the components/users view:

ErrorException Undefined variable: users (View: C:\wamp64\www\fastclick\resources\views\components\users.blade.php)
Raed AlGherbawi's avatar

I know that but I checked the code several times but I don't know what caused this problem

@foreach($users as $key => $user)

<li>{{$user->id}}</li>

<h1>{{$user->name}}</h1>

<h2>{{$user->body}}</h2>

@endforeach

Raed AlGherbawi's avatar
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Users extends Controller
{
    public function getAllUser(){
    $users = DB::table('users')->get();
    return view('pages/users',compact('users'));
    }
}


<section class="inner-page">
  
@foreach($users as $key => $user)
    <li>{{$user->id}}</li>
    <h1>{{$user->name}}</h1>
    <h2>{{$user->body}}</h2>
  @endforeach
  
</section>

use App\Http\Controllers\Users;
Route::get('users',[Users::class,'getAllUser'])->name('all.user');
tykus's avatar

@rado82r@gmail.com okay better!

So this is all of markup in the resources/views/pages/users.blade.php view template? And you still are getting that error?

Raed AlGherbawi's avatar

this all code on page


  <main id="main">

<!-- ======= Breadcrumbs ======= -->
<section class="breadcrumbs">
  <div class="container">

    <ol>
      <li><a href="{{ route('home') }}">Home</a></li>
      <li>Users</li>
    </ol>
    <h2>Users</h2>

  </div>
</section>
<!-- End Breadcrumbs -->

<section class="inner-page">
  
@foreach($users as $key => $user)
    <li>{{$user->id}}</li>
    <h1>{{$user->name}}</h1>
    <h2>{{$user->body}}</h2>
  @endforeach
  
</section>

</main>
<!-- End #main -->

tykus's avatar

@rado82r@gmail.com based on the code and error message your showing; I don't think you're actually hitting that Controller action. Do you have another route defined for the /users URL?

Raed AlGherbawi's avatar

My friend, when I delete this code

@foreach($users as $key => $user)
    <li>{{$user->id}}</li>
    <h1>{{$user->name}}</h1>
    <h2>{{$user->body}}</h2>
  @endforeach

from the page, the link works and the page works fine

tykus's avatar

@rado82r@gmail.com are you actually coming through that controller action:

public function getAllUser(){
    $users = DB::table('users')->get();
    dd($users);
    return view('pages/users',compact('users'));
}
Raed AlGherbawi's avatar
Illuminate\Support\Collection {#310 ▼
  #items: array:5 [▼
    0 => {#312 ▼
      +"id": 1
      +"name": "Raed"
      +"body": "Raed Ahmed Ismael El Gherbawi"
    }
    1 => {#314 ▼
      +"id": 2
      +"name": "Taghreed"
      +"body": "taghreed Tawafik Khalil El Khaldy"
    }
    2 => {#315 ▼
      +"id": 3
      +"name": "Zaina"
      +"body": "Zaina Raed Ahmed El Gherbawi"
    }
    3 => {#316 ▼
      +"id": 4
      +"name": "Mohammed"
      +"body": "Mohammed Raed Ahmed El Gherbawi"
    }
    4 => {#317 ▼
      +"id": 5
      +"name": "Zain"
      +"body": "Zain Raed Ahmed El Gherbawi"
    }
  ]
  #escapeWhenCastingToString: false

tykus's avatar

@rado82r@gmail.com this has me stumped. Sanity check:

  • the data is there
  • clearly it is passed to the resources/views/pages/users view template
  • in resources/views/pages/users view template you say you have a loop that cannot work
@foreach($users as $key => $user)
  • you are getting an ErrorException for a completely different view
ErrorException Undefined variable: users (View: C:\wamp64\www\fastclick\resources\views\components\users.blade.php)
Raed AlGherbawi's avatar

This result appeared when I added dd($users);

this is test record on database

This means that the problem is in the viewn page only

correct??

tykus's avatar

@rado82r@gmail.com what does the resources\views\components\users.blade.php template look like?

Raed AlGherbawi's avatar

The template works well and there is no problem when deleting the code, the template appears excellent

tykus's avatar

@rado82r@gmail.com which template - are you telling me that resources\views\components\users.blade.php and resources\views\pages\users.blade.php are the same?

Raed AlGherbawi's avatar

@tykus

resources\views\components\users.blade.php


  <main id="main">

<!-- ======= Breadcrumbs ======= -->
<section class="breadcrumbs">
  <div class="container">

    <ol>
      <li><a href="{{ route('home') }}">Home</a></li>
      <li>Users</li>
    </ol>
    <h2>Users</h2>

  </div>
</section>
<!-- End Breadcrumbs -->

<section>

@foreach($users as $key => $user)
    <li>{{$user->id}}</li>
    <h1>{{$user->name}}</h1>
    <h2>{{$user->body}}</h2>
@endforeach
 
</section>

</main>
<!-- End #main -->

resources\views\pages\users.blade.php


<x-header title="Users Page"/>

<x-users/>

<x-footer/>

tykus's avatar

@rado82r@gmail.com seriously how many times have I asked you - you were giving the wrong answer all along????

Pass the data to the component

<x-users :users=“$users” />
anilkumarthakur60's avatar
    public function getAllUser(){
    $users = DB::table('users')->get();
    return view('pages.users',compact('users'));
    }

users.blade.php inside pages folder should be accessed by pages.users inside your controller why you are doing pages/users

1 like
tykus's avatar

@rado82r@gmail.com . or / doesn’t matter for the view template path. You were not passing the data from the page to the component

1 like
anilkumarthakur60's avatar

you are using component-based or controller based

for controller based

if your users.blade.php file is inside views/pages/users.blade.php then


   public function getAllUser(){
    $users = DB::table('users')->get();
    return view('pages.users',compact('users'));
    }

if your users.blade.php file is inside views/components/users.blade.php then

   public function getAllUser(){
    $users = DB::table('users')->get();
    return view('components.users',compact('users'));
    }

for component-based

 public function render()
    {
        $users = DB::table('users')->get();

        return view('components.users', [
            'users' => $users
        ]);
    }

used.blade.php inside views/components/users.blade.php

1 like
Raed AlGherbawi's avatar

it`s sloved

the sloved it`s

add this code in app/views/components/users.blade.php

  $users = DB::table('users')->get();
        return view('components.users', [
            'users' => $users
        ]);

and it`s worked on my templete

Thank you all my friends for your support

tykus's avatar

@rado82r@gmail.com I don’t think you have learned anything to be honest with you.

Your solution bypasses the conventional MVC approach

1 like
Raed AlGherbawi's avatar

@tykus

The solution because I am using view in one place and content page in another I have to put the variables identifier in that path app/views/components/users.blade.php

Please or to participate in this conversation.