Laravel Microsoft Graph

The Microsoft Graph is a powerful tool that allows developers to access and utilize the data from various Microsoft services, including Office 365, Windows, and security platforms.

Laravel Microsoft Graph, created by developer David Carr is a package that provides seamless integration with the Microsoft Graph REST API, making it easier for developers to interact with some of Microsoft's cloud services.

Key Features

The following features are currently available:

Installation and Configuration

First, install the package via Composer:

composer require dcblogdev/laravel-microsoft-graph

Next, publish the configuration file:

php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="config"

This will create a config file at config/msgraph.php.

Then update your .env file with the following credentials:

MSGRAPH_CLIENT_ID=your-client-id
MSGRAPH_SECRET_ID=your-client-secret
MSGRAPH_TENANT_ID=your-tenant-id

MSGRAPH_OAUTH_URL=https://yourdomain.com/msgraph/oauth
MSGRAPH_LANDING_URL=https://yourdomain.com/msgraph

And then publish the migration:

php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="migrations"

Note: If you are using the Microsoft Graph as an authentication service you will also need to add a listener:

php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="Listeners"

This will create app/Listeners/NewMicrosoft365SignInListener.php with a default listener and then you can add it to your app/Providers/AppServiceProvider:

    public function boot(): void
    {
        Event::listen(
            NewMicrosoft365SignInEvent::class,
            [NewMicrosoft365SignInListener::class, 'handle']
        );
    }

Examples

1. Authenticating a User

use Dcblogdev\MsGraph\MsGraph;

// Redirect user to Microsoft login page
return (new MsGraph())->connect();

// Handle callback after login
$msGraph = new MsGraph();
$user = $msGraph->get('/me');

return $user;

2. Sending an Email

use Dcblogdev\MsGraph\MsGraph;

$msGraph = new MsGraph();

$msGraph->emails()
        ->to(['test@example.com'])
        ->subject('Hello from Laravel News')
        ->body('This is a test email from Laravel News.')
        ->send();

3. Listing OneDrive Files

use Dcblogdev\MsGraph\MsGraph;

$msGraph = new MsGraph();
$files = $msGraph->files()->getFiles('/path/to/folder', 'me');
# The second parameter determines whether to use your one drive or a Groups
# $files = $msGraph->files()->getFiles('/path/to/folder', "groups/$groupId");

return $files;

If you are in the M365 ecosystem and using Laravel then the Laravel Microsoft Graph package could be an indispensable tool for you. Its simple API integration and user-friendly methods make it an excellent choice for Laravel projects requiring access to Microsoft Graph functionality.

For more information read the full documentation and view the source code on Github.


The post Laravel Microsoft Graph appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.