PHPackages                             prasadchinwal/microsoft-graph - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. prasadchinwal/microsoft-graph

ActiveLibrary[API Development](/categories/api)

prasadchinwal/microsoft-graph
=============================

Package for working with Microsoft Graph API

v3.3(3mo ago)16.2k↓50%1MITPHPPHP ^8.2|^8.3|^8.4|^8.5

Since Oct 10Pushed 4w ago1 watchersCompare

[ Source](https://github.com/PrasadChinwal/laravel-microsoft-graph)[ Packagist](https://packagist.org/packages/prasadchinwal/microsoft-graph)[ RSS](/packages/prasadchinwal-microsoft-graph/feed)WikiDiscussions 3.x Synced 2d ago

READMEChangelog (10)Dependencies (12)Versions (16)Used By (0)

Laravel Microsoft Graph
=======================

[](#laravel-microsoft-graph)

[![Latest Version on Packagist](https://camo.githubusercontent.com/29e7ba51246b15690a4311f9999aebef8f6b64e40939a09c86dbef8585166fae/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7072617361646368696e77616c2f6d6963726f736f66742d67726170682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/prasadchinwal/microsoft-graph)[![Total Downloads](https://camo.githubusercontent.com/c61ff35cc3acb20ec9d264a8c9ba3923e4ad14f1ac8637fd866f51a3dd1dd524/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7072617361646368696e77616c2f6d6963726f736f66742d67726170682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/prasadchinwal/microsoft-graph)[![License](https://camo.githubusercontent.com/2b28c4278db01206254f0c09fe26b8cae8bda4162ca4506da7da3e82638ca8e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7072617361646368696e77616c2f6d6963726f736f66742d67726170682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/prasadchinwal/microsoft-graph)

A comprehensive Laravel wrapper for Microsoft Graph API that provides an elegant, fluent interface for interacting with Microsoft 365 services including users, calendars, events, mail, and more.

Features
--------

[](#features)

- 🚀 **Easy Integration** - Simple, Laravel-style fluent API
- ⚡ **Performance** - Automatic OAuth token caching with expiry handling
- 🔒 **Type Safe** - Full support for PHP 8.2+ with strict types and return type declarations
- 🎯 **Data Objects** - Strongly-typed responses using Spatie Laravel Data
- 🛡️ **Error Handling** - Custom exceptions with clear, actionable error messages
- 📧 **Email &amp; Calendar** - Full support for Outlook, calendars, events, and mail
- 👥 **User Management** - Complete user CRUD operations with license management
- 🖼️ **Profile Photos** - Upload, download, and delete user profile photos
- 📎 **Attachment Management** - Upload, download, and manage file, item, and reference attachments
- 📝 **Query Filters** - OData query filtering for events and calendars
- 🔧 **Laravel 10-12** - Supports Laravel 10.x, 11.x, and 12.x

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Laravel 10.0, 11.0, or 12.0
- Microsoft Azure App Registration with appropriate permissions

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require prasadchinwal/microsoft-graph
```

Publish the configuration file:

```
php artisan vendor:publish --tag=microsoft-graph-config
```

This will create a `config/microsoft-graph.php` file in your application.

Configuration
-------------

[](#configuration)

### 1. Azure App Registration

[](#1-azure-app-registration)

Before using this package, you need to register an application in the [Azure Portal](https://portal.azure.com):

1. Navigate to **Azure Active Directory** &gt; **App registrations**
2. Click **New registration**
3. Give your app a name and select supported account types
4. Click **Register**
5. Note down the **Application (client) ID** and **Directory (tenant) ID**
6. Go to **Certificates &amp; secrets** &gt; **New client secret**
7. Create a new secret and note down its value
8. Go to **API permissions** and add the required Microsoft Graph permissions:
    - `User.Read.All`
    - `User.ReadWrite.All`
    - `Calendars.Read`
    - `Calendars.ReadWrite`
    - `Mail.Read`
    - `Mail.Send`
    - Grant admin consent for your organization

### 2. Environment Configuration

[](#2-environment-configuration)

Add the following to your `.env` file:

```
MICROSOFT_GRAPH_TENANT_ID=your-tenant-id
MICROSOFT_GRAPH_CLIENT_ID=your-client-id
MICROSOFT_GRAPH_CLIENT_SECRET=your-client-secret
```

### 3. Configuration File

[](#3-configuration-file)

The `config/microsoft-graph.php` file contains:

```
return [
    'tenant_id' => env('MICROSOFT_GRAPH_TENANT_ID', null),
    'client_id' => env('MICROSOFT_GRAPH_CLIENT_ID', null),
    'client_secret' => env('MICROSOFT_GRAPH_CLIENT_SECRET', null),
    'timezone' => env('APP_TIMEZONE', 'UTC'),
];
```

**Note:** The package will validate configuration on boot and throw a `ConfigurationException` if required values are missing.

Usage
-----

[](#usage)

### Initialization

[](#initialization)

The package can be used via the Facade or by instantiating the class:

```
use PrasadChinwal\MicrosoftGraph\Facades\MicrosoftGraph;

// Via Facade
$graph = app('graph');

// Or direct instantiation
$graph = new \PrasadChinwal\MicrosoftGraph\MicrosoftGraph();
```

---

User Management
---------------

[](#user-management)

### Get All Users

[](#get-all-users)

Retrieve all users in your organization.

**API Reference:** [List Users](https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http)

```
use PrasadChinwal\MicrosoftGraph\Facades\MicrosoftGraph;

$users = MicrosoftGraph::users()->get();

foreach ($users as $user) {
    echo $user->displayName . ' - ' . $user->mail;
}
```

### Get a Specific User

[](#get-a-specific-user)

Retrieve a user by their email address (User Principal Name).

**API Reference:** [Get User](https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http)

```
$user = MicrosoftGraph::users()->find('john.doe@company.com');

echo $user->displayName;        // John Doe
echo $user->jobTitle;           // Software Engineer
echo $user->department;         // Engineering
echo $user->mobilePhone;        // +1 234 567 8900
```

### Update a User

[](#update-a-user)

Update user properties using the User builder.

**API Reference:** [Update User](https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=http)

```
use PrasadChinwal\MicrosoftGraph\Builder\User\User;

$userUpdate = new User();
$userUpdate->displayName = 'Jane Smith';
$userUpdate->jobTitle = 'Senior Software Engineer';
$userUpdate->department = 'Engineering';
$userUpdate->mobilePhone = '+1 234 567 8901';

MicrosoftGraph::users()->update('jane.smith@company.com', $userUpdate);
```

### Get User Licenses

[](#get-user-licenses)

Retrieve license details for a user.

**API Reference:** [List License Details](https://learn.microsoft.com/en-us/graph/api/user-list-licensedetails?view=graph-rest-1.0&tabs=http)

```
$licenses = MicrosoftGraph::users()
    ->withEmail('john.doe@company.com')
    ->getLicenses();

foreach ($licenses as $license) {
    echo $license->skuPartNumber;
    foreach ($license->servicePlans as $plan) {
        echo $plan->servicePlanName;
    }
}
```

### Assign License to User

[](#assign-license-to-user)

Assign one or more licenses to a user.

**API Reference:** [Assign License](https://learn.microsoft.com/en-us/graph/api/user-assignlicense?view=graph-rest-1.0&tabs=http)

```
use PrasadChinwal\MicrosoftGraph\Builder\License\AssignLicenseBuilder;
use PrasadChinwal\MicrosoftGraph\Builder\License\NewLicense;
use PrasadChinwal\MicrosoftGraph\Builder\License\NewLicenseCollection;

$license = new NewLicense();
$license->skuId = 'sku-guid-here';

$licenseCollection = new NewLicenseCollection([$license]);

$builder = new AssignLicenseBuilder(
    addLicenses: $licenseCollection,
    removeLicenses: []
);

$updatedUser = MicrosoftGraph::users()
    ->withEmail('john.doe@company.com')
    ->assignLicense($builder);
```

---

Profile Photos
--------------

[](#profile-photos)

### Get User Profile Photo

[](#get-user-profile-photo)

Download a user's profile photo.

**API Reference:** [Get Photo](https://learn.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0&tabs=http)

```
$response = MicrosoftGraph::users()
    ->withEmail('john.doe@company.com')
    ->getPhoto();

// Save to file
file_put_contents('profile.jpg', $response->body());

// Or return as response
return response($response->body())
    ->header('Content-Type', 'image/jpeg');
```

### Update User Profile Photo

[](#update-user-profile-photo)

Upload a new profile photo for a user.

**API Reference:** [Update Photo](https://learn.microsoft.com/en-us/graph/api/profilephoto-update?view=graph-rest-1.0&tabs=http)

```
$imageData = file_get_contents('/path/to/photo.jpg');

MicrosoftGraph::users()
    ->withEmail('john.doe@company.com')
    ->updatePhoto($imageData);
```

### Delete User Profile Photo

[](#delete-user-profile-photo)

Remove a user's profile photo.

**API Reference:** [Delete Photo](https://learn.microsoft.com/en-us/graph/api/profilephoto-delete?view=graph-rest-1.0&tabs=http)

```
MicrosoftGraph::users()
    ->withEmail('john.doe@company.com')
    ->deletePhoto();
```

---

Calendar Management
-------------------

[](#calendar-management)

### Get User Calendars

[](#get-user-calendars)

Retrieve all calendars for a user.

**API Reference:** [List Calendars](https://learn.microsoft.com/en-us/graph/api/user-list-calendars?view=graph-rest-1.0&tabs=http)

```
$calendars = MicrosoftGraph::calendar()
    ->for('john.doe@company.com')
    ->get();

foreach ($calendars as $calendar) {
    echo $calendar->name;
}
```

### Get User Schedule

[](#get-user-schedule)

Get free/busy schedule information for one or more users.

**API Reference:** [Get Schedule](https://learn.microsoft.com/en-us/graph/api/calendar-getschedule?view=graph-rest-1.0&tabs=http)

```
use Carbon\Carbon;

$users = ['john.doe@company.com', 'jane.smith@company.com'];

$schedule = MicrosoftGraph::calendar()
    ->for('requester@company.com')
    ->schedule(
        users: $users,
        from: Carbon::now(),
        to: Carbon::now()->addDays(7),
        timezone: 'America/Chicago',
        interval: 60
    );
```

### Get Calendar View

[](#get-calendar-view)

Get calendar events within a specific time range.

**API Reference:** [List Calendar View](https://learn.microsoft.com/en-us/graph/api/calendar-list-calendarview?view=graph-rest-1.0&tabs=http)

```
use Carbon\Carbon;

$events = MicrosoftGraph::calendar()
    ->for('john.doe@company.com')
    ->view(
        start: Carbon::now()->toIso8601String(),
        end: Carbon::now()->addMonth()->toIso8601String()
    );
```

---

Event Management
----------------

[](#event-management)

### Get User Events

[](#get-user-events)

Retrieve calendar events for a user.

**API Reference:** [List Events](https://learn.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=http)

```
$events = MicrosoftGraph::event()
    ->for('john.doe@company.com')
    ->get();

foreach ($events as $event) {
    echo $event->subject;
    echo $event->start->dateTime;
    echo $event->end->dateTime;
}
```

### Filter Events

[](#filter-events)

Use OData filters to query specific events.

**API Reference:** [Filter Query Parameter](https://learn.microsoft.com/en-us/graph/filter-query-parameter?tabs=http)

```
// Get events within a date range
$events = MicrosoftGraph::event()
    ->for('john.doe@company.com')
    ->where('start/dateTime', 'ge', '2024-01-01')
    ->where('end/dateTime', 'le', '2024-12-31')
    ->get();

// Use OR conditions
$events = MicrosoftGraph::event()
    ->for('john.doe@company.com')
    ->where('subject', 'eq', 'Team Meeting')
    ->orWhere('subject', 'eq', 'All Hands')
    ->get();
```

### Create an Event

[](#create-an-event)

Create a new calendar event using a CalendarEvent class.

**API Reference:** [Create Event](https://learn.microsoft.com/en-us/graph/api/user-post-events?view=graph-rest-1.0&tabs=http)

**Step 1:** Generate an event class:

```
php artisan make:graph-event TeamMeetingEvent
```

**Step 2:** Configure the event class:

```
