Passing data from controller to view in Laravel

前端 未结 9 1719
我在风中等你
我在风中等你 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:27
    $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 讨论(0)
  • 2020-11-30 10:31

    try with this code :

    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]
    
    0 讨论(0)
  • 2020-11-30 10:33

    Try with this code:

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

    Or if you want to pass more variables into view:

    return View::make('user/regprofile', array
        (
            'students'    =>  $students,
            'variable_1'  =>  $variable_1,
            'variable_2'  =>  $variable_2
        )
    );
    
    0 讨论(0)
  • 2020-11-30 10:42

    For Passing a single variable to view.

    Inside Your controller create a method like:

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

    In Your route

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

    In Your View Welcome.blade.php. You can echo your variable like {{ $title }}

    For An Array(multiple values) change,sleep method to :

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

    You can access you variable like {{ $author }}.

    0 讨论(0)
  • 2020-11-30 10:44

    Can you give this a try,

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

    While, you can set multiple variables something like this,

    $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);
    
    0 讨论(0)
  • 2020-11-30 10:44

    You can try this as well:

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

    Also, use this variable in your view.blade file to get students name and other columns:

    {{$students['name']}}
    
    0 讨论(0)
提交回复
热议问题