Hey guys I just started learning how to use Laravel and when I tried running the code below I get:
Undefined variable error
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
@foreach ($tasks as $task)
<li>{{ $task->Todo }}</li>
@endforeach
</ul>
</body>
</html>
this is the code used in the web.php
file:
web.php
Route::get('/tasks', function () {
$tasks = DB::table('tasks')->get();
//return $tasks;
return view('welcome',compact($tasks));
});
I discovered that if I use the $GLOBALS['variable'];
to replace the $tasks
variable in both files it works.
But in the example video from laracasts they didn't make use of the $GLOBALS['variable'];
This is the error I get:
"Undefined variable: tasks (View: C:\Users\Friday\Documents\Documentations\laraprojects\BrainGearesources\views\welcome.blade.php)"