繁体   English   中英

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

[英]Passing data from controller to view in Laravel

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

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

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

在我看来,我有这个代码:

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

我收到此错误: Undefined variable: students (View:regprofile.blade.php)

你可以试试这个吗

return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));

虽然,您可以像这样设置多个变量,

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

用于传递单个变量以查看。

在您的控制器内部创建一个方法,如:

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

在你的路线

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

在您Welcome.blade.php 你可以像{{ $title }}这样回显你的变量

对于数组(多个值)更改,睡眠方法为:

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

您可以像{{ $author }}一样访问您的变量。

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

为了将单个变量传递给 view

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

为了将多个变量传递给 view

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

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

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

你也可以试试这个:

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

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

{{$students['name']}}

尝试使用此代码:

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

或者,如果您想将更多变量传递到视图中:

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

在 Laravel 5.6 中:

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

尝试使用此代码:

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]
$books[] = [
            'title' => 'Mytitle',
            'author' => 'MyAuthor,
            
        ];

//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));

----------------------------------------------------


//to use this on myView.blade.php
<script>
    myVariable = {!! json_encode($books) !!};
    console.log(myVariable);
</script>

在laravel 8 及更高版本中,您可以通过这种方式进行路由绑定。

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

在视图文件中,您可以像下面那样访问它。

@foreach($students as $student)
        {{$student->name}}
@endforeach

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM