无法将laravel数组传递给视图

时间:2018-06-08 11:56:41

标签: php laravel

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

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

但这会产生以下错误:

  

尝试获取非对象的属性

2 个答案:

答案 0 :(得分:3)

那是因为数组不是对象。

// -> 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>

答案 1 :(得分:1)

原因

Column

无效是因为 - &gt; operator用于访问Object。请注意,您收到以下错误:

{{ $new_recordss->ProcedureName }}

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

Trying to get property of non-object

此处,&#39; ProcedureName&#39;只是关键名称,我们告诉数组获取它的值。我希望这能回答你的问题。