Passing data from controller to view in Laravel

前端 未结 9 1720
我在风中等你
我在风中等你 2020-11-30 10:26

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.

相关标签:
9条回答
  • 2020-11-30 10:44

    In Laravel 5.6:

    $variable = model_name::find($id);
    return view('view')->with ('variable',$variable);
    
    0 讨论(0)
  • 2020-11-30 10:51

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

    For passing single variable to view,

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

    For passing multiple variable to 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
    
    0 讨论(0)
  • 2020-11-30 10:51
    public function showstudents() {
         $students = DB::table('student')->get();
         return (View::make("user/regprofile", compact('student')));
    }
    
    0 讨论(0)
提交回复
热议问题