在Laravel中从控制器向视图传递数据

27

我是 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)

10个回答

28

你能试一下这个吗,

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

2
哎呀,我在 compact('students) 后面漏了一个括号。谢谢。 - VP1234

20

将单个变量传递到视图中。

在您的控制器中创建一个类似于以下的方法:

function sleep()
{
        return view('welcome')->with('title','My App');
}
在您的路线中。
Route::get('/sleep', 'TestController@sleep');

在您的视图Welcome.blade.php中,您可以像这样使用{{ $title }}来输出您的变量。

对于数组(多个值)的更改,请使用sleep方法:

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

你可以像这样访问你的变量:{{ $author }}


1
更详细的回答。 - rahul

10

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

要将单个变量传递到视图中

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

用于将多个变量传递给视图的方法

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

而且在视图中,您可以轻松地遍历该变量。

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

4
你也可以尝试这个:
public function showstudents(){
   $students = DB::table('student')->get();
   return view("user/regprofile", ['students'=>$students]);
}

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

{{$students['name']}}

1
在Laravel 5.6中:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);

1

尝试使用此代码:

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

或者,如果你想向视图传递更多的变量:

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

0
在 Laravel 8 及以上版本中,您可以通过以下方式进行路由绑定。
public function showstudents() {
    $students = DB::table('student')->get();
    return view("user/regprofile",['students'=>$students]);
}

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

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

这不是路由模型绑定。 - Hashim Aziz

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

1
请考虑添加一些注释,以解释您的代码如何回答问题,从而改进此答案。 - Eric Hauenstein

0
$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>

0

尝试使用这段代码:

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]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接