VP1*_*234 15 php laravel laravel-blade
嘿伙计们我是laravel的新手,我一直试图将表'student'的所有记录存储到变量中,然后将该变量传递给视图,以便我可以显示它们.
我有一个控制器 - ProfileController,里面有一个函数:
public function showstudents()
{
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我有这个代码
<html>
<head></head>
<body> Hi {{Auth::user()->fullname}}
@foreach ($students as $student)
{{$student->name}}
@endforeach
@stop
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我收到此错误:未定义的变量:学生(查看:regprofile.blade.php)
Irf*_*med 15
你能尝试一下吗?
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
Run Code Online (Sandbox Code Playgroud)
同时,您可以设置多个这样的变量,
$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);
Run Code Online (Sandbox Code Playgroud)
Bug*_*xer 10
用于传递单个变量以进行查看.
在您的控制器内创建一个方法,如:
function sleep()
{
return view('welcome')->with('title','My App');
}
Run Code Online (Sandbox Code Playgroud)
在你的路线
Route::get('/sleep', 'TestController@sleep');
Run Code Online (Sandbox Code Playgroud)
在你的视图中Welcome.blade.php.你可以回复你的变量{{ $title }}
对于一个数组(多个值)更改,睡眠方法为:
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
Run Code Online (Sandbox Code Playgroud)
你可以访问你的变量{{ $author }}.
传递单个或多个变量以从控制器查看的最佳且简单的方法是使用compact()方法。
为了将单个变量传递给 view,
return view("user/regprofile",compact('students'));
Run Code Online (Sandbox Code Playgroud)
为了将多个变量传递给 view,
return view("user/regprofile",compact('students','teachers','others'));
Run Code Online (Sandbox Code Playgroud)
在视图中,您可以轻松地遍历变量,
@foreach($students as $student)
{{$student}}
@endforeach
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
83899 次 |
| 最近记录: |