我是 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/