0

Why I get this error?

ErrorException (E_ERROR) Undefined variable: users (View: /Users/acny/Desktop/Actor/resources/views/home.blade.php)

function of controller :

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

and .blade :

 @foreach ($users as $user)
    {{ $user }}
 @endforeach

Thank for your help!

5
  • 1
    before return view write dd($users) and check if $users exist or not Commented Sep 25, 2018 at 11:27
  • Need more info for better clarity. Commented Sep 25, 2018 at 11:29
  • 1
    I think it is because, You've sent the users data to the the "index.blade.php" view and you are using it in the "home.blade.php". Commented Sep 25, 2018 at 12:42
  • I fixed it but still get this error Commented Sep 25, 2018 at 12:52
  • Show me your index.blade.php and home.blade.php file. Commented Sep 25, 2018 at 12:56

2 Answers 2

2

You are getting error from home.blade.php, but getData() try to load inuser/index.blade.php.

Are you sure your request going through method getData()?

Sign up to request clarification or add additional context in comments.

3 Comments

You are right ! I fixed it but still get this error ! What should I do to make the get() method work properly?
Can i see web.php?
Please look the above code again. I fixed it but still not working!
0

web.php :

Route::get('/', function () {
    return view('welcome'); });

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

2 Comments

Here is your problem :) Add Route::get('/link-to-your-page', 'HomeController@getData');. More information here: laravel.com/docs/5.7/routing
My problem solved. I write new route with new view for UserController

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.