php - 将数据从 Controller 传递到 Laravel 中的 View

标签 php laravel laravel-blade

我是 Laravel 的新手,我一直在尝试将表 'student' 的所有记录存储到一个变量,然后将该变量传递给一个 View ,以便我可以显示它们。

我有一个 Controller - ProfileController,里面有一个函数:

public function showstudents() {
    $students = DB::table('student')->get();
    return View::make("user/regprofile")->with('students',$students);
}

在我看来,我有这段代码:

<html>
    <head>
        //---HTML Head Part
    </head>
    <body>
        Hi {{ Auth::user()->fullname }}
        @foreach ($students as $student)
            {{ $student->name }}
        @endforeach
        @stop
    </body>
</html>

我收到此错误:Undefined variable: students (View:regprofile.blade.php)

最佳答案

你能试试这个吗,

return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));

同时,您可以像这样设置多个变量,

$instructors="";
$instituitions="";

$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);

return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);

关于php - 将数据从 Controller 传递到 Laravel 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220482/

相关文章:

php - jQuery Ajax Json 响应 - 检查是否为 null

laravel - 带有 vue 2 组件的 vue 路由器不适用于 laravel 5.8

laravel - 如何在测试完成后修复 "Can' t 关闭模拟,它在另一个测试中可见”

php - 如何在 laravel 中使用编码和解码

php - Laravel 5.2 'Master Layout' 变量传递

javascript - 将 javascript 值插入表单字段

php - 如何在 PHP 中捕获未设置的 REQUEST 值

javascript - laravel 将集合分配给 javascript 变量时出错

PHP Zip 文件为空?

css - 如何根据foreach循环中的键更改更改表 'td'颜色