I'm learning Laravel, and I've been trying to pass data between a controller and a view, but I'm still getting the same error on the page. Does anyone know how to fix this? Even with this small code, it doesn't seem to work. Do I need to make a configuration or something?
I've tried
->with('message', 'Hi Victoria')
view
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>App title</title>
<html>
<body>
<h1>Hello, {{ $message }}</h1>
</body>
</html>
</html>
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Hello;
class HelloWorld extends Controller
{
public function sayHello()
{
return view('hello', ['message' => 'Hi Victoria']);
}
}
web.php
Route::get('/', function(){
return view('hello');
});
The problem is that you have written a controller, but you are not using it. In your route ( web.php ), you have returned the view.
You should take a look at Laravel's documentation regarding how to make a route point to a controller method
So, you would need to change your route in web.php
to
use App\Http\Controllers\HelloWorld;
Route::get('/', [HelloWorld::class, 'sayHello']);