PHP code example of ismailelbery / laravel-ldap-sync
1. Go to this page and download the library: Download ismailelbery/laravel-ldap-sync library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Illuminate\Support\Facades\Schedule;
Schedule::command('ldap:sync')->dailyAt('02:00');
use IsmailElbery\LdapSync\Events\UserSynced;
use IsmailElbery\LdapSync\Events\SyncCompleted;
Event::listen(UserSynced::class, function (UserSynced $event) {
// $event->user, $event->wasRecentlyCreated
});
Event::listen(SyncCompleted::class, function (SyncCompleted $event) {
// $event->report->created, ->updated, ->disabled
});
use IsmailElbery\LdapSync\Facades\LdapUserSearch;
// By username (sAMAccountName) — exact match
$user = LdapUserSearch::byUsername('i.elbery');
// By email — exact match
$user = LdapUserSearch::byEmail('[email protected]');
// By name — partial match, searches displayName (EN and AR attributes)
$users = LdapUserSearch::byName('Ismail'); // returns Collection
$users = LdapUserSearch::byName('إسماعيل'); // Arabic works too
// By department
$users = LdapUserSearch::byDepartment('Digitization');
// Smart search — detects whether the term looks like an email,
// username, or name, and searches the right attributes
$users = LdapUserSearch::search('[email protected]');
$users = LdapUserSearch::search('Ismail');
// Fluent combination
$users = LdapUserSearch::query()
->inOu('ou=IT,ou=Departments')
->department('Infrastructure')
->name('Ahmed')
->limit(20)
->get();
$user->username; // sAMAccountName
$user->name; // displayName
$user->nameAr; // Arabic display name (if configured)
$user->email;
$user->department;
$user->title;
$user->phone;
$user->dn; // distinguished name
$user->guid; // objectGUID
$user->managerDn; // raw manager attribute
use IsmailElbery\LdapSync\Facades\LdapUserSearch;
$user = LdapUserSearch::byUsername('i.elbery');
// Direct manager — single LdapUserDto (or null)
$manager = LdapUserSearch::directManagerOf($user);
$manager = LdapUserSearch::directManagerOf('i.elbery'); // username works too
// Full management chain — ordered Collection from direct manager
// up to the top of the hierarchy (cycle-safe)
$chain = LdapUserSearch::managersOf('i.elbery');
foreach ($chain as $level => $manager) {
echo "{$level}: {$manager->name} ({$manager->title})";
}
// 0: Ahmed Saleh (Section Head)
// 1: Khalid Omar (Department Director)
// 2: Fahad Al-Otaibi (Deputy Governor)
// Limit chain depth (e.g. for approval workflows needing only 2 levels)
$chain = LdapUserSearch::managersOf('i.elbery', maxDepth: 2);
// Inverse: who reports directly to this user?
$reports = LdapUserSearch::directReportsOf('k.omar');