php Laravel Ldaprecord -在刀片中显示从Active Directory检索的数据

jgovgodb  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(95)

当我从Active Directory检索数据时,例如:

$users = User::whereStartsWith('cn', $request->search)
        ->limit(3)
        ->get();

我检索一个集合,它看起来像这样:

LdapRecord\Models\Collection {#350 ▼
  #items: array:3 [▼
    0 => LdapRecord\Models\ActiveDirectory\User {#353 ▼
      #passwordAttribute: "unicodepwd"
      #passwordHashMethod: "encode"
      #dates: array:7 [▶]
      #defaultDates: array:3 [▶]
      #sidKey: "objectsid"
      +exists: true
      +wasRecentlyCreated: false
      +wasRecentlyRenamed: false
      #dn: "CN=DOE John,DC=my,DC=domain"
      #in: null
      #connection: null
      #guidKey: "objectguid"
      #modifications: []
      #original: array:95 [▶]
      #attributes: array:95 [▶]
      #casts: []
      #appends: []
      #dateFormat: null
      #hidden: []
      #visible: []
    }
    1 => LdapRecord\Models\ActiveDirectory\User {#354 ▼
      #passwordAttribute: "unicodepwd"
      #passwordHashMethod: "encode"
      #dates: array:7 [▶]
      #defaultDates: array:3 [▶]
      #sidKey: "objectsid"
      +exists: true
      +wasRecentlyCreated: false
      +wasRecentlyRenamed: false
      #dn: "CN=SMITH Jan,DC=my,DC=domain"
      #in: null
      #connection: null
      #guidKey: "objectguid"
      #modifications: []
      #original: array:94 [▶]
      #attributes: array:94 [▶]
      #casts: []
      #appends: []
      #dateFormat: null
      #hidden: []
      #visible: []
    }
    2 => LdapRecord\Models\ActiveDirectory\User {#357 ▼
      #passwordAttribute: "unicodepwd"
      #passwordHashMethod: "encode"
      #dates: array:7 [▶]
      #defaultDates: array:3 [▶]
      #sidKey: "objectsid"
      +exists: true
      +wasRecentlyCreated: false
      +wasRecentlyRenamed: false
      #dn: "CN=LORD Coeh,DC=my,DC=domain"
      #in: null
      #connection: null
      #guidKey: "objectguid"
      #modifications: []
      #original: array:66 [▶]
      #attributes: array:66 [▶]
      #casts: []
      #appends: []
      #dateFormat: null
      #hidden: []
      #visible: []
    }
  ]
  #escapeWhenCastingToString: false
}

在这个集合中,在名为#original(也是一个数组)的受保护属性下,有像sAMAccountNamedcmail等字段(取决于用户的数量不同)。
我无法在刀片中使用foreach显示它,如下所示:

@foreach ($users as $user)
  <tr>
      <td>{{ $user->cn }}</td>
      <td>{{ $user->samaccountname }}</td>
      <td>{{ $user->mail }}</td>
  </tr>
@endforeach

因为我得到了一个错误:

htmlspecialchars() expects parameter 1 to be string, array given

如何打印元素,这是缝内收集?

gfttwv5a

gfttwv5a1#

其中一个属性是一个数组,必须在html处理器{{ }}使用它之前将其内聚为String。
下面的代码检查属性是否为数组,然后执行implode函数,否则返回属性的原始值。

@foreach ($users as $user)
  <tr>
      <td>{{ (is_array($user->cn)) ? implode($user->cn) : $user->cn }}</td>
      <td>{{ (is_array($user->samaccountname)) ? implode($user->samaccountname) : $user->samaccountname }}</td>
      <td>{{ (is_array($user->mail)) ? implode($user->mail) : $user->mail }}</td>
  </tr>
@endforeach

相关问题