Home >Backend Development >PHP Tutorial >laravel向视图传递变量的几种方法

laravel向视图传递变量的几种方法

WBOY
WBOYOriginal
2016-06-20 12:34:231048browse

    public function test1(){        $first='xiaoqiang';        $last='gongmiao';        return view('test.welcome')->with('first',$first);        /*            鉴于,laravel变量的怪现象,将视图中执行的代码,放在这里            <h2>{{$first}}</h2>        */        // return view('test.welcome')->with('last',$last);        //不能两次return不同的值    }    public function test2(){        // $first='xiaoqiang';        $name=[];        $name['a']='xiaoqiang';        $name['b']='xiaoqiang';        return view('test.welcome',$name);        /*        <h2>{{$a}}</h2>        <h2>{{$b}}</h2>        $a$b可单独放置也可以一起放,但要注意$name不能直接使用        */    }    public function test3(){        $name=[];        $name['a']='xiaoqiang';        $name['b']='xiaoqiang';        return view('test.welcome')->with('name',$name);        /*        <h2>{{$name['a']}}</h2>        <h2>{{$name['b']}}</h2>        $name['a']$name['b']可单独放置也可以一起放,但要注意$name和$name[]不能直接使用        */    }    public function test4(){        $first='xiaoqiang';        $last='gongmiao';        return view('test.welcome',compact('last','first'));        //->with('name',$name)        //name是你视图中的变量名. $name是自己设置的变量.    }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn