Laravel 8 从控制器发送数据到 blade
Laravel 8 sending data from controller to blade
嘿,我需要从我的控制器发送数据以供查看。以下是用户类型
时的路由代码
http://127.0.0.1:8000/About一个控制器将被激活。但是抛出了一个错误,我不知道该怎么做。欢迎任何提示或解决方案:)
代码如下
路线
Route::get('About','App\Http\Controllers\Users@about');
控制器
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class Users extends Controller
{
public function about(){
$name = "Tony Stack";
return view('aboutus')->with('name', $name);
}
}
Blade代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>About Us page</h1>
<?php echo $name;?>
<br>
<br> <br>
<a href="About">About Us</a> <br>
<a href="Contact"> Contact</a><br>
<a href="/"> HOME</a>
</body>
</html>
错误
ErrorException
Undefined variable: name (View: C:\xampp\htdocs\storify\resources\views\aboutus.blade.php)
http://127.0.0.1:8000/About
使用这个
return view('aboutus', compact('name'));
在 compact 的参数中,您必须传递没有 $
登录 qoutes 的变量名。然后在 blade 文件中,您可以访问传递的值,如 $name
嘿,我需要从我的控制器发送数据以供查看。以下是用户类型
时的路由代码http://127.0.0.1:8000/About一个控制器将被激活。但是抛出了一个错误,我不知道该怎么做。欢迎任何提示或解决方案:)
代码如下
路线
Route::get('About','App\Http\Controllers\Users@about');
控制器
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class Users extends Controller
{
public function about(){
$name = "Tony Stack";
return view('aboutus')->with('name', $name);
}
}
Blade代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>About Us page</h1>
<?php echo $name;?>
<br>
<br> <br>
<a href="About">About Us</a> <br>
<a href="Contact"> Contact</a><br>
<a href="/"> HOME</a>
</body>
</html>
错误
ErrorException
Undefined variable: name (View: C:\xampp\htdocs\storify\resources\views\aboutus.blade.php)
http://127.0.0.1:8000/About
使用这个
return view('aboutus', compact('name'));
在 compact 的参数中,您必须传递没有 $
登录 qoutes 的变量名。然后在 blade 文件中,您可以访问传递的值,如 $name