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

发布于 2025-01-16 05:37:40 字数 2698 浏览 0 评论 0原文

当我从 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 在 Blade 中显示它,例如:

@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

如何打印在集合中缝制的元素?

When I retrieve data from Active Directory like:

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

I retrieve an collection which looks like this:

    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
}

Inside this collection, under protected property called #original(which is also an array), there are fields like sAMAccountName, dc, mail etc (there are different amount depends of user).

I can't display it in blade using foreach like:

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

Because I got an error:

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

How to print elements, which are sewn inside collection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2025-01-23 05:37:40

其中一个属性是一个数组,必须先内爆为字符串,然后才能被 html 处理器 {{ }} 使用。

以下代码检查属性是否为数组,然后执行 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

One of the properties are an array and must be imploded to a String before it can be used by the html processor {{ }}.

The following code checks if the property is an array, then it performs the implode function, otherwise it returns the original value of property.

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文