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

时间:2015-05-13 16:29:01

标签: php laravel laravel-blade

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

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

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

在我看来,我有这段代码

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

    @endforeach


    @stop

    </body>
    </html>

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

10 个答案:

答案 0 :(得分:14)

你可以尝试一下吗,

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

答案 1 :(得分:8)

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

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

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 }}等变量。

答案 2 :(得分:1)

在Laravel 5.6中:

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

答案 3 :(得分:1)

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

用于传递单个变量以查看
return view("user/regprofile",compact('students'));

用于将多个变量传递给视图
return view("user/regprofile",compact('students','teachers','others'));

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

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

答案 4 :(得分:0)

尝试使用此代码:

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

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

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

答案 5 :(得分:0)

您也可以尝试以下方法:

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

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

    {{$students['name']}}

答案 6 :(得分:0)

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

答案 7 :(得分: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]

答案 8 :(得分: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>

答案 9 :(得分:0)

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

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

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

@foreach($students as $student)
        {{$student->name}}
@endforeach
相关问题