Home >php教程 >php手册 >Laravel 5框架学习之向视图传送数据(进阶篇)

Laravel 5框架学习之向视图传送数据(进阶篇)

WBOY
WBOYOriginal
2016-06-06 20:05:221182browse

上篇文章我们介绍了Laravel5框架中如何向视图中传送数据,今天我们来研究下如何向视图中传送array,非常的详细,推荐给有需要的小伙伴参考下。

我们不仅仅可以向视图传送一个数据,同样我们可以传送Array

 代码如下:

    public function about()
    {
        return view('pages.about')->with([
            'first' => 'Zhang',
            'last' => 'Jinglin'
        ]);
    }

About {{ $first }} {{ $last }}


一种简洁的方式是这样:

代码如下:

    public function about()
    {
        $data = [];
        $data['first'] = 'Zhang';
        $data['last'] = 'Jinglin';
        return view('pages.about', $data);
    }

结果相同,更简单的是这样:

代码如下:

    public function about()
    {
        $first = 'Zhang';
        $last = 'Jinglin';
        return view('pages.about', compact('first', 'last'));
    }

compact 把参数变成数组,extract相反。可以查看php手册了解 compact ,顺道了解一下 extract

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