【问题标题】:Passing data from controller to view in Laravel将数据从控制器传递到 Laravel 中的视图
【发布时间】:2015-07-25 01:47:46
【问题描述】:

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

【问题讨论】:

    标签: php laravel laravel-blade


    【解决方案1】:

    你可以试试这个,

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

    【讨论】:

    • 糟糕,我在 compact('students) 之后缺少一个括号。谢谢
    【解决方案2】:

    用于将单个变量传递给查看。

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

    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 }}一样访问你的变量。

    【讨论】:

    • 更好的详细答案。
    【解决方案3】:

    从控制器传递单个或多个变量到视图的最佳和简单的方法是使用 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']}}
      

      【讨论】:

        【解决方案5】:

        试试这个代码:

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

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

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

        【讨论】:

          【解决方案6】:

          在 Laravel 5.6 中:

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

          【讨论】:

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

            【讨论】:

            • 请考虑添加一些 cmets 来解释您的代码如何回答问题,以改进此答案。
            【解决方案8】:

            试试这段代码:

            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]
            

            【讨论】:

              【解决方案9】:
              $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>
              

              【讨论】:

                【解决方案10】:

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

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

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

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

                【讨论】:

                • 这不是路由模型绑定。
                猜你喜欢
                • 2017-01-29
                • 2019-04-15
                • 1970-01-01
                • 2017-12-02
                • 1970-01-01
                • 2017-09-03
                • 2016-01-30
                • 2018-06-11
                相关资源
                最近更新 更多