im new at Laravel, and im making a CRUD following this tutorial:
https://appdividend.com/2020/03/13/laravel-7-crud-example-laravel-7-tutorial-step-by-step/
first i made it on my own, and then i just copied the code from GitHub, in both cases im getting in the variable inside the @foreach:
FacadeIgnitionExceptionsViewException Undefined variable: coronacases
I searched a bit and tryed some other solutions with no luck, so i started a new project with just index function to see if data is passed from controler to view.
This is my code, Controller:
// TestController namespace AppHttpControllers; use IlluminateHttpRequest; class TestController extends Controller { public function index() { $name = 'hugo'; return view('test', compact('name')); }
Alternative sintaxys tested in Controller:
/*** * return view('/test', compact('name')); * return view('name', ['name' => 'James']); with out $name = 'hugo'; earier. * return view('test', $name]); * return view('test')->with('name', $data); * return view('test')->with('name', 'hugo'); with out $name = 'hugo'; earier. * other solutions tryed here dosnt seems to change anything. ***/
Routes:
Route::get('/', function () { return view('test'); }); Route::resource('test', 'TestController');
Alternative Routes Tested:
/*** * Route::get('/test', 'TestController@index'); * * Route::get('/', function () { * return view('test', ['name' => 'James']); *}); //this one worked, but its not getting data from Controller, so its now what i need * *Route::get('/', function () { * return view('test', ['name' => $data]); *}); //didnt work * ***/ // I think that the problem is here, im having trouble understanding Routes at Laravel Docs
View:
<html> <body> <h1>Hello, {{ $name }}</h1> </body> </html>
Also tryed at View:
/*** * $data * <?php echo $data ?> * {{ $name ?? '' }} ***/
All this Test, and im getting same error, Im missing Something and i cant figure it out… or i have some trouble with Laravel,the sure things is that im stuck.
Complementary info:
php artisan route:list resoult:
Environment information
Laravel version: 7.16.1, Laravel locale: en, Laravel config cached: false, PHP version: 7.4.6.
Request
URL http://127.0.0.1:8000/ Method GET
Headers
host 127.0.0.1:8000 user-agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0
I also tryed with php artisan view:cache && php artisan view:clear
Advertisement
Answer
You’ve defined a function called index
on your TestController
but you have not used it.
In your route, you are not using the controller and that function , so you get this error.
Your method is like : route > view
It should be : route > controller > view
So Change your route :
Route::get('/', function () { return view('test'); });
to
Route::get('/', 'TestController@index');
Cheers 🙂