【问题标题】:Unable to pass laravel array on to the view无法将 laravel 数组传递给视图
【发布时间】:2018-06-08 11:56:41
【问题描述】:

我有一个数组,我想从控制器返回到视图。但无法检索它。 如果我转储我的数组,它看起来像这样:

array:121 [▼
  0 => array:10 [▼
    "ProcedureName" => "eye"
    "Status" => "referred"
    "PreferredDate" => "12/12/2018"
    "PreferredCity" => "Bangalore"
    "BookingId" => "EZBK000126"
    "Documents" => ""
    "Name" => "vik kon"
    "Age" => 62
    "Gender" => "male"
    "Mob" => "1110002223"
  ]
  1 => array:10 [▼
    "ProcedureName" => "eye"
    "Status" => "referred"
    "PreferredDate" => "12/12/2018"
    "PreferredCity" => "mysore"
    "BookingId" => "EZBK000125"
    "Documents" => ""
    "Name" => "vik kon"
    "Age" => 62
    "Gender" => "male"
    "Mob" => "9146178526"
  ]

所以我想让它显示在我的视图中。目前我的代码如下所示:

@foreach($new_records as $new_recordss)

    <tr>

      <td scope="col">{{ $new_recordss->ProcedureName }}</td>

      <td scope="col">{{ $new_recordss->Status }}</td>

      <td scope="col">{{ $new_recordss->Age }}</td>

      <td scope="col"><div class="btn-group">

   </div>
 </td>


    </tr>
    @endforeach

但这会产生以下错误:

试图获取非对象的属性

【问题讨论】:

  • 共享控制器方法,你是如何通过$new_records 来查看的?似乎数组不是对象
  • @gaan10 是的,请分享控制器方法。
  • 他为什么要这么做?错误是Trying to get property of non-object,考虑到提供的代码,这完全有道理。

标签: php laravel


【解决方案1】:

那是因为数组不是对象。

// -> is for object
<td scope="col">{{ $new_recordss->ProcedureName }}</td>

// [] is for array
 <td scope="col">{{ $new_recordss['ProcedureName'] }}</td>

因此这应该有效:

<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
<td scope="col">{{ $new_recordss['Status'] }}</td>
<td scope="col">{{ $new_recordss['Age'] }}</td>

【讨论】:

  • 没问题。考虑打勾作为答案,以便其他人受益!
  • 当然,会这样做
【解决方案2】:

原因

{{ $new_recordss->ProcedureName }}

不起作用是因为 -> 运算符用于访问对象。请注意,您收到以下错误:

Trying to get property of non-object

因为您有一个包含 121 个数组的数组。因此,为了访问这个关联数组,您可以使用

{{ $new_recordss['ProcedureName'] }}

这里,“ProcedureName”只是键名,我们告诉数组获取它的值。我希望这能回答你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2015-01-11
    • 2019-08-17
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    相关资源
    最近更新 更多