Passing data from controller to view in Laravel

前端 未结 9 1770
我在风中等你
我在风中等你 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: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 }}.

提交回复
热议问题