英文:
Passing arrays from laravel controller to view
问题
I can help you with the translation. Here's the translated code part:
这是数组:
$postData = array(
'source_addr' => 'JJA ENT',
'encoding' => 0,
'schedule_time' => '',
'message' => 'Hello World',
'recipients' => array(
array('recipient_id' => '1', 'dest_addr' => '233554463045'),
array('recipient_id' => '2', 'dest_addr' => '255700000011')
)
);
在控制器中返回视图的方式:
return view('main', compact('postData'));
在 Blade 文件中:
<body>
{{$postData['message']}}
这将输出数组中与消息相对应的值。如果你现在希望以类似的方式获取“recipients”的属性,但是像消息那样传递它们会导致错误,你可以尝试如下的方式在控制器和 Blade 文件中使用:
在控制器中返回视图的方式:
return view('main', compact('postData'));
在 Blade 文件中:
<body>
@foreach ($postData['recipients'] as $recipient)
{{$recipient['recipient_id']}}: {{$recipient['dest_addr']}}
@endforeach
</body>
这将允许你访问“recipients”数组中的属性并在 Blade 文件中显示它们。
英文:
Please l am working with array in laravel. I want a way to access some values and display them as forms fields in blade.php file.
This is the array :
$postData = array(
'source_addr' => 'JJA ENT',
'encoding' => 0,
'schedule_time' => '',
'message' => 'Hello World',
'recipients' =>
[array('recipient_id' => '1', 'dest_addr' => '233554463045'), array('recipient_id' => '2', 'dest_addr' => '255700000011')]
);
This is how I return the view in this controller :
return view('main',compact('postData'));
In the blade file
<body>
{{$postData['message']}}
This output the value corresponding to the message in the array.
I now want a way to get the attributes of the "recipients" too but when I pass the recipients like the way I did to the message, it returned errors.
This is how I return the view in the controller
return view('main',compact('postData'));
In the blade
<body>
{{$postData['message']}}
答案1
得分: 1
你可以使用 foreach
循环来在 Blade 模板中获取数组数值。以下是一个示例:
{{$postData['message']}}
@foreach ($postData['recipients'] as $recipient)
<input type="text" name="recipient_id[]" value="{{$recipient['recipient_id']}}">
<input type="text" name="dest_addr[]" value="{{$recipient['dest_addr']}}">
@endforeach
请注意,这是 Blade 模板中的 PHP 代码,用于遍历 $postData
数组中的数据。
英文:
You can use foreach
Loop to get array values in blade. Here is an example
{{$postData['message']}}
@foreach ($postData['recipients'] as $recipient)
<input type="text" name="recipient_id[]" value="{{$recipient['recipient_id']}}">
<input type="text" name="dest_addr[]" value="{{$recipient['dest_addr']}}">
@endforeach
答案2
得分: 1
不应混合不同的数组表示法。只会让自己感到困惑而没有理由。array()
用于与不再维护的非常旧的 PHP 版本保持兼容性。你应该使用 [ ]
代替。
$postData = [
'source_addr' => 'JJA ENT',
'encoding' => 0,
'schedule_time' => '',
'message' => 'Hello World',
'recipients' => [
[
'recipient_id' => '1',
'dest_addr' => '233554463045'
],
[
'recipient_id' => '2',
'dest_addr' => '255700000011'
]
]
];
return view('main', [
'postData' => $postData
]);
<p>Source Address: {{ $postData['source_addr'] }}</p>
<p>Encoding: {{ $postData['encoding'] }}</p>
<p>Schedule Time: {{ $postData['schedule_time'] }}</p>
<p>Message: {{ $postData['message'] }}</p>
<p>Recipients</p>
<ul>
@foreach ($postData['recipients'] as $recipient)
<li>ID: {{ $recipient['recipient_id'] }}. Address: {{ $recipient['dest_addr'] }}</li>
@endforeach
</ul>
英文:
You shouldn't mix different array notations. You'll only confuse yourself for no reason. array()
was used to keep compatibility with very old PHP versions that are no longer maintained. You should use [ ]
instead.
$postData = [
'source_addr' => 'JJA ENT',
'encoding' => 0,
'schedule_time' => '',
'message' => 'Hello World',
'recipients' => [
[
'recipient_id' => '1',
'dest_addr' => '233554463045'
],
[
'recipient_id' => '2',
'dest_addr' => '255700000011'
]
]
];
return view('main', [
'postData' => $postData
]);
<p>Source Address: {{ $postData['source_addr'] }}</p>
<p>Encoding: {{ $postData['encoding'] }}</p>
<p>Schedule Time: {{ $postData['schedule_time'] }}</p>
<p>Message: {{ $postData['message'] }}</p>
<p>Recipients</p>
<ul>
@foreach ($postData['recipients'] as $recipient)
<li>ID: {{ $recipient['recipient_id'] }}. Address: {{ $recipient['dest_addr'] }}</li>
@endforeach
</ul>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论