将数据从控制器传递到Laravel中的视图

时间:2022-05-20 13:58:10

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.

嘿伙计们我是laravel的新手,我一直试图将表'student'的所有记录存储到变量中,然后将该变量传递给视图,以便我可以显示它们。

I have a controller - ProfileController and inside that a function:

我有一个控制器 - ProfileController,里面有一个函数:

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

In my view I have this code

在我看来,我有这个代码

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

    @endforeach


    @stop

    </body>
    </html>

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

我收到此错误:未定义的变量:学生(查看:regprofile.blade.php)

7 个解决方案

#1


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

#2


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 }}

在您的视图Welcome.blade.php中。您可以像{{$ 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 }}.

您可以访问{{$ author}}之类的变量。

#3


In Laravel 5.6:

在Laravel 5.6中:

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

#4


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

从控制器传递单个或多个变量的最佳和最简单的方法是使用compact()方法。

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

要将单个变量传递给视图,请返回视图(“user / regprofile”,compact('students'));

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

要将多个变量传递给视图,请返回视图(“user / regprofile”,compact('students','teachers','others'));

And in view you can easile loop through the variable,

在视图中,您可以轻松循环变量,

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

@foreach(学生为$学生){{$ student}} @endforeach

#5


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

#6


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:

并在view.blade文件中使用此变量来获取学生姓名和其他列:

    {{$students['name']}}

#7


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

#1


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

#2


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 }}

在您的视图Welcome.blade.php中。您可以像{{$ 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 }}.

您可以访问{{$ author}}之类的变量。

#3


In Laravel 5.6:

在Laravel 5.6中:

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

#4


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

从控制器传递单个或多个变量的最佳和最简单的方法是使用compact()方法。

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

要将单个变量传递给视图,请返回视图(“user / regprofile”,compact('students'));

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

要将多个变量传递给视图,请返回视图(“user / regprofile”,compact('students','teachers','others'));

And in view you can easile loop through the variable,

在视图中,您可以轻松循环变量,

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

@foreach(学生为$学生){{$ student}} @endforeach

#5


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

#6


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:

并在view.blade文件中使用此变量来获取学生姓名和其他列:

    {{$students['name']}}

#7


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