Home  >  Article  >  Backend Development  >  Learning to transfer array to view in Laravel5 framework

Learning to transfer array to view in Laravel5 framework

不言
不言Original
2018-06-13 10:04:561699browse

In the last article, we introduced how to transfer data to the view in the Laravel5 framework. Today we will study how to transfer array to the view. It is very detailed and is recommended for reference by friends in need.

We can not only send a data to the view, we can also send an Array

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

A concise way is this:

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

The result is the same, even simpler is Like this:

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

compact turns the parameters into arrays, and extract does the opposite. You can check the php manual to learn about compact and learn about extract by the way.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Learning the model and controller of the Laravel 5 framework and the basic process of the view

The above is the detailed content of Learning to transfer array to view in Laravel5 framework. For more information, please follow other related articles on the PHP Chinese website!

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