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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30220482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:38:22  来源:igfitidea点击:

Passing data from controller to view in Laravel

phplaravellaravel-blade

提问by VP1234

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)

回答by Irfan Ahmed

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

回答by Bugfixer

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

回答by Anand Mainali

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

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

For passing single variable to view,

为了将单个变量传递给 view

return view("user/regprofile",compact('students'));

For passing multiple variable to view,

为了将多个变量传递给 view

return view("user/regprofile",compact('students','teachers','others'));

And in view, you can easily loop through the variable,

在视图中,您可以轻松地遍历变量,

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

回答by Emmanuel Uko

You can try this as well:

你也可以试试这个:

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

Also, use this variable in your view.bladefile to get students name and other columns:

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

{{$students['name']}}

回答by Emmanuel Uko

In Laravel 5.6:

在 Laravel 5.6 中:

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

回答by 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
    )
);

回答by sathish

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

回答by Manirul Islam

try with this code :

尝试使用此代码:

Controller:
-----------------------------
 $fromdate=date('Y-m-d',strtotime(Input::get('fromdate'))); 
        $todate=date('Y-m-d',strtotime(Input::get('todate'))); 

 $datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To 
        return view('inventoryreport/inventoryreportview', compact('datas'));

View Page : 
@foreach($datas as $student)
   {{$student}}

@endforeach
[Link here]