VP1234 VP1234 - 6 months ago 88
PHP Question

Passing data from controller to view in Laravel

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.

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);

In my view I have this code

<body> Hi {{Auth::user()->fullname}}
@foreach ($students as $student)




I am receiving this error : Undefined variable: students (View:regprofile.blade.php)


Can you give this a try,

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,


$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);