How does laravel5 pass a multi-dimensional array to the blade template?

Source: Internet
Author: User
An error occurred while passing arrays to the blade template using the view () method in the controller. Details: {code ...} code in the blade template: {code ...} error message: {code ...} if {code ...} the $ last of the blade template can receive data normally. Is there an error when using the view () method in controller to pass an array to the blade template. The details are as follows:

$data['first'] = array(array('lily', 'lucy'));return view('result', $data)

Code in the blade template:

@foreach($first as $value)...@endforeach

The error message is as follows:

ErrorException in helpers.php line 454:htmlentities() expects parameter 1 to be string, array given (View: /home/wwwroot/laravel/resources/views/result.blade.php)

If

$data['last'] = array('lily', 'lucy');return view('result', $last)

The $ last of the blade template can receive data normally.

Didn't laravel pass the multi-dimensional array method? Or is my code incorrect?

After reading the document, the statement is as follows:

If you want to, another way is to directly pass an array in the second parameter of the view auxiliary method:

$view = view('greetings', $data);

If you use the above method to transmit data parameters,$dataIt must be the array data corresponding to the key/value. In this way, you can use the corresponding key in the view to obtain the value, for example:{{ $key }}Yes$data['$key']Corresponding data.

This does not cover multi-dimensional arrays.

Reply content:

An error occurred while passing arrays to the blade template using the view () method in the controller. The details are as follows:

$data['first'] = array(array('lily', 'lucy'));return view('result', $data)

Code in the blade template:

@foreach($first as $value)...@endforeach

The error message is as follows:

ErrorException in helpers.php line 454:htmlentities() expects parameter 1 to be string, array given (View: /home/wwwroot/laravel/resources/views/result.blade.php)

If

$data['last'] = array('lily', 'lucy');return view('result', $last)

The $ last of the blade template can receive data normally.

Didn't laravel pass the multi-dimensional array method? Or is my code incorrect?

After reading the document, the statement is as follows:

If you want to, another way is to directly pass an array in the second parameter of the view auxiliary method:

$view = view('greetings', $data);

If you use the above method to transmit data parameters,$dataIt must be the array data corresponding to the key/value. In this way, you can use the corresponding key in the view to obtain the value, for example:{{ $key }}Yes$data['$key']Corresponding data.

This does not cover multi-dimensional arrays.

First of all, I am very happy that there are Laravel problems.

Then let me talk about my thoughts:

In fact, the error message is as follows: it does not mean that your multi-dimensional array is not passed in the past, and this multi-dimensional array has actually been passed to result. blade. php, but you may encounter errors during the foreach loop:

You may write something like this:

"@foreach($first as $value) {{ $value }}@endforeach"

It should actually be like this:

"@foreach($first as $value) {{ $value[0] }}@endforeach"

Because the $ value here is actually an array, while the blade Engine cannot directly output the array when parsing {}, blade wants {} To be a string.

So the problem is not that you didn't pass it over, but that you didn't output it correctly. You can try to debug it like this:

"@foreach($first as $value) {{ dump($value) }}@endforeach"

Happy Hacking

I made a mistake. Yes, it can be passed. You need to write with ('last', $ data), that is, specify the last. If it is automatically recognized, blade can recognize $ data ['first'], but the following array won't work.

Such a simple question... You all know that it is a two-dimensional array. A foreach is not enough... For example:
@ Foreach ($ logs as $ log)
@ Foreach ($ log as $ k => $ v)
{{$ K }=>{{ $ v }};
@ Endforeach
@ Endforeach

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.