Passing arrays from laravel controller to view

huangapple go评论72阅读模式
英文:

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(
            &#39;source_addr&#39; =&gt; &#39;JJA ENT&#39;,

    &#39;encoding&#39; =&gt; 0,
            &#39;schedule_time&#39; =&gt; &#39;&#39;,
            &#39;message&#39; =&gt; &#39;Hello World&#39;,
            &#39;recipients&#39; =&gt; 

   [array(&#39;recipient_id&#39; =&gt;       &#39;1&#39;, &#39;dest_addr&#39; =&gt; &#39;233554463045&#39;),   array(&#39;recipient_id&#39; =&gt; &#39;2&#39;, &#39;dest_addr&#39; =&gt; &#39;255700000011&#39;)]
        );

This is how I return the view in this controller :

    return     view(&#39;main&#39;,compact(&#39;postData&#39;));

In the blade file

&lt;body&gt;
    {{$postData[&#39;message&#39;]}}

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(&#39;main&#39;,compact(&#39;postData&#39;));

In the blade

&lt;body&gt;
    {{$postData[&#39;message&#39;]}}

答案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[&#39;message&#39;]}}

@foreach ($postData[&#39;recipients&#39;] as $recipient)
    &lt;input type=&quot;text&quot; name=&quot;recipient_id[]&quot; value=&quot;{{$recipient[&#39;recipient_id&#39;]}}&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;dest_addr[]&quot; value=&quot;{{$recipient[&#39;dest_addr&#39;]}}&quot;&gt;
@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 = [
    &#39;source_addr&#39; =&gt; &#39;JJA ENT&#39;,
    &#39;encoding&#39; =&gt; 0,
    &#39;schedule_time&#39; =&gt; &#39;&#39;,
    &#39;message&#39; =&gt; &#39;Hello World&#39;,
    &#39;recipients&#39; =&gt; [
        [
            &#39;recipient_id&#39; =&gt; &#39;1&#39;,
            &#39;dest_addr&#39; =&gt; &#39;233554463045&#39;
        ],
        [
            &#39;recipient_id&#39; =&gt; &#39;2&#39;,
            &#39;dest_addr&#39; =&gt; &#39;255700000011&#39;
        ]
    ]
];

return view(&#39;main&#39;, [
    &#39;postData&#39; =&gt; $postData
]);
&lt;p&gt;Source Address: {{ $postData[&#39;source_addr&#39;] }}&lt;/p&gt;
&lt;p&gt;Encoding: {{ $postData[&#39;encoding&#39;] }}&lt;/p&gt;
&lt;p&gt;Schedule Time: {{ $postData[&#39;schedule_time&#39;] }}&lt;/p&gt;
&lt;p&gt;Message: {{ $postData[&#39;message&#39;] }}&lt;/p&gt;
&lt;p&gt;Recipients&lt;/p&gt;
&lt;ul&gt;
  @foreach ($postData[&#39;recipients&#39;] as $recipient)
    &lt;li&gt;ID: {{ $recipient[&#39;recipient_id&#39;] }}. Address: {{ $recipient[&#39;dest_addr&#39;] }}&lt;/li&gt;
  @endforeach
&lt;/ul&gt;

</details>



huangapple
  • 本文由 发表于 2023年4月7日 00:03:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定