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,$data
It 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,$data
It 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