Reputation: 35
I want to send my data from controller to xedit.blade.php, but I get the same error:
Undefined variable: users
in controller:
public function index3()
{
$users=User::all();
return view('xedit')->with('users' => $users);
}
Routes:
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
Route::get('edit','Admin\UsersController@index3');
and I want to use $users in blade.Maybe there is a Route problem?
Upvotes: 0
Views: 999
Reputation: 585
Use this code.
Route::get('/admin/edit','AdminController@index3')->name('edit');
It calls the AdminController's index3() and the function returns the view.
public function index3()
{
$users=User::all();
return view('xedit')->with('users' => $users);
}
The below code won't call the index3() in AdminController but returns the view directly with empty data.
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
Upvotes: 0
Reputation: 5270
You have to change
Route::get('edit','Admin\UsersController@index3')->name('edit');
Upvotes: 1
Reputation: 93
The problem is you are using the same view xedit for both of your routes admin/edit & /edit .
So, when you visit admin/edit $users
variable in the xedit view file getting no value which means it's undefined. Try to check if $users
variable is defined before using that.
Upvotes: 0
Reputation: 41
maybe this route
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
This route doesn't contain $users, maybe this is why it's undefined.
Upvotes: 0