Passing data from controller to view in Laravel

守給你的承諾、 提交于 2019-11-27 05:04:00

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,

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

For Passing a single variable to view.

Inside Your controller create a method like:

function sleep()
{
        return view('welcome')->with('title','My App');
}

In Your route

Route::get('/sleep', 'TestController@sleep');

In Your View Welcome.blade.php. You can echo your variable like {{ $title }}

For An Array(multiple values) change,sleep method to :

function sleep()
{
        $data = array(
            'title'=>'My App',
            'Description'=>'This is New Application',
            'author'=>'foo'
            );
        return view('welcome')->with($data);
}

You can access you variable like {{ $author }}.

DelvinDuel

In Laravel 5.6:

$variable = model_name::find($id);
return view('view')->with ('variable',$variable);

The best and easy way to pass single or multiple variables to view from controller is to use compact() method.

For passing single variable to view,
return view("user/regprofile",compact('students'));

For passing multiple variable to view,
return view("user/regprofile",compact('students','teachers','others'));

And in view you can easile loop through the variable,

@foreach($students as $student) {{$student}} @endforeach

Vanndy

Try with this code:

return View::make('user/regprofile', array
    (
        'students' => $students
    )
);

Or if you want to pass more variables into view:

return View::make('user/regprofile', array
    (
        'students'    =>  $students,
        'variable_1'  =>  $variable_1,
        'variable_2'  =>  $variable_2
    )
);
Emmanuel Uko

You can try this as well:

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

and use this variable in your view.blade file to get students name and other columns:

    {{$students['name']}}
sathish
public function showstudents() {
     $students = DB::table('student')->get();
     return (View::make("user/regprofile", compact('student')));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!