Home > Article > Backend Development > Learning to transfer array to view in Laravel5 framework
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['first'] = 'Zhang'; $data['last'] = 'Jinglin'; return view('pages.about', $data); }
The result is the same, even simpler is Like this:
public function about() { $first = 'Zhang'; $last = 'Jinglin'; return view('pages.about', compact('first', 'last')); }
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!