将数据从控制器传递到Laravel中的视图 [英] Passing data from controller to view in Laravel
本文介绍了将数据从控制器传递到Laravel中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是laravel的新手,我一直试图将表'student'的所有记录存储到一个变量中,然后将该变量传递给视图,以便我可以显示它们.
Hey guys I am new to laravel and I have been trying to store all records of table 'student' to a variable and then pass that variable to a view so that I can display them.
我有一个控制器-ProfileController,里面有一个函数:
I have a controller - ProfileController and inside that a function:
public function showstudents()
{
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
我认为我有此代码
<html>
<head></head>
<body> Hi {{Auth::user()->fullname}}
@foreach ($students as $student)
{{$student->name}}
@endforeach
@stop
</body>
</html>
我收到此错误:未定义变量:学生(View:regprofile.blade.php)
I am receiving this error : Undefined variable: students (View:regprofile.blade.php)
推荐答案
可以尝试一下,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
同时,您可以设置多个类似这样的变量
While, you can set multiple variables something like this,
$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);
这篇关于将数据从控制器传递到Laravel中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文