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???
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
this source code to main page
The page is located in a folder called pages@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)
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>
@rado82r@gmail.com can you repost your route, controller and view(s) using properly formatted code
controller code
@rado82r@gmail.com 🤷♂️
<?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');
@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?
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 -->
@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?
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
@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'));
}
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
@rado82r@gmail.com this has me stumped. Sanity check:
- the data is there
- clearly it is passed to the
resources/views/pages/usersview template - in
resources/views/pages/usersview 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)
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??
This is the problem I have
@rado82r@gmail.com what does the resources\views\components\users.blade.php template look like?
The template works well and there is no problem when deleting the code, the template appears excellent
@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?
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/>
@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” />
@rado82r@gmail.com Try that 👆
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
I changed it but same problem :(
@rado82r@gmail.com . or / doesn’t matter for the view template path. You were not passing the data from the page to the component
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
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
@rado82r@gmail.com it’s been frustrating thanks
@tykus Don't say that, we all learn from our mistakes :)
@rado82r@gmail.com I don’t think you have learned anything to be honest with you.
Your solution bypasses the conventional MVC approach
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.