> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel Notification for Discord(Webhook)

> Een pakket om eenvoudig notificaties vanuit Laravel te versturen via Discord-webhooks. Geen bot, geen WebSocket: alleen een webhook-URL instellen en je kunt direct aan de slag.

## Overzicht

[revolution/laravel-notification-discord-webhook](https://github.com/invokable/laravel-notification-discord-webhook) is een pakket dat via Discord-webhooks notificaties verstuurt vanuit Laravel Notifications naar Discord.

De officiële Bot API van Discord vereist het in stand houden van een WebSocket-verbinding en is complex om op te zetten. Met een webhook hoef je alleen een URL op te halen om notificaties te kunnen versturen. Als je alleen app-events wilt melden in het Discord-kanaal van je team, is een webhook de beste keuze.

<Info>
  Dit pakket vereist PHP 8.3 of hoger en Laravel 12.0 of hoger.
</Info>

## Installatie

```shell theme={null}
composer require revolution/laravel-notification-discord-webhook
```

## Configuratie

### Een Discord-webhook-URL ophalen

Je kunt een webhook-URL aanmaken via **Instellingen → Integraties → Webhooks** van je Discord-server. Zie de [officiële Discord-gids](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) voor meer details.

### config/services.php

```php theme={null}
'discord' => [
    'webhook' => env('DISCORD_WEBHOOK'),
],
```

### .env

```dotenv theme={null}
DISCORD_WEBHOOK=https://discord.com/api/webhooks/...
```

## Basisgebruik

### Een Notification-klasse aanmaken

Implementeer de method `toDiscordWebhook()` en geef in `via()` `DiscordChannel::class` terug.

```php theme={null}
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordChannel;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;

class DiscordNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(protected string $content)
    {
        //
    }

    public function via($notifiable): array
    {
        return [DiscordChannel::class];
    }

    public function toDiscordWebhook(object $notifiable): DiscordMessage
    {
        return DiscordMessage::create(content: $this->content);
    }
}
```

## Verzendpatronen

### On-demand notificaties

Gebruik `Notification::route()` als je een notificatie wilt versturen zonder koppeling aan een specifiek usermodel.

```php theme={null}
use Illuminate\Support\Facades\Notification;

Notification::route('discord-webhook', config('services.discord.webhook'))
            ->notify(new DiscordNotification('De deploy is voltooid.'));
```

### Notificaties vanuit het User-model

Definieer `routeNotificationForDiscordWebhook()` als je per gebruiker een andere webhook-URL wilt gebruiken.

```php theme={null}
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDiscordWebhook($notification): string
    {
        return $this->discord_webhook;
    }
}
```

```php theme={null}
$user->notify(new DiscordNotification('Testnotificatie'));
```

## Berichttypen

### Tekstberichten

De eenvoudigste manier van versturen.

```php theme={null}
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create(content: $this->content);
}
```

### Embed-berichten

Met `DiscordEmbed` kun je rijke weergaven maken met een titel en URL.

```php theme={null}
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordEmbed;

public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
                          ->embed(
                              DiscordEmbed::make(
                                  title: 'INFO',
                                  description: $this->content,
                                  url: route('home'),
                              )
                          );
}
```

### Bestandsbijlagen

Met `DiscordAttachment` kun je bestanden als bijlage meesturen. `content` (de inhoud van het bestand) en `filename` zijn verplicht.

```php theme={null}
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordAttachment;
use Illuminate\Support\Facades\Storage;

public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
        ->file(
            DiscordAttachment::make(
                content: Storage::get('test.png'),
                filename: 'test.png',
                description: 'test',
                filetype: 'image/png'
            )
        );
}
```

### Verwijzen naar bestanden binnen een embed

Je kunt in de `image` of `thumbnail` van een embed naar bijlagen verwijzen met `attachment://bestandsnaam`.

```php theme={null}
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordAttachment;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordEmbed;
use Illuminate\Support\Facades\Storage;

public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
                          ->embed(
                              DiscordEmbed::make(
                                  title: 'test',
                                  description: $this->content,
                                  image: 'attachment://test.jpg',
                                  thumbnail: 'attachment://test2.jpg',
                              )
                          )
                          ->file(DiscordAttachment::make(
                              content: Storage::get('test.jpg'),
                              filename: 'test.jpg',
                              description: 'test',
                              filetype: 'image/jpg'
                          ))
                          ->file(new DiscordAttachment(
                              content: Storage::get('test2.jpg'),
                              filename: 'test2.jpg',
                              description: 'test2',
                              filetype: 'image/jpg'
                          ));
}
```

### Een willekeurige payload versturen

Met `->with()` kun je elke parameter van de Discord-webhook rechtstreeks opgeven.

```php theme={null}
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
                          ->with([
                              'content' => $this->content,
                              'embeds' => [[]],
                          ]);
}
```

<Tip>
  Zie de [officiële API-documentatie van Discord](https://discord.com/developers/docs/resources/webhook#execute-webhook) voor de parameters die je met `->with()` kunt opgeven.
</Tip>

## Samenvatting

Discord-bots zijn krachtig, maar als je alleen notificaties wilt versturen, is een webhook veel eenvoudiger. Met dit pakket integreer je naadloos met het notificatiesysteem van Laravel en meld je app-events snel in het Discord-kanaal van je team.

<Info>
  Zie de [GitHub-repository](https://github.com/invokable/laravel-notification-discord-webhook) voor de meest recente informatie.
</Info>


## Related topics

- [我的包](/zh-CN/packages/index.md)
- [Tutorial - Laravel Console Starter](/nl/packages/laravel-console-starter/tutorial.md)
- [Envoy](/nl/envoy.md)
- [Socialite for Discord](/nl/packages/socialite-discord.md)
- [LINE SDK for Laravel](/nl/packages/laravel-line-sdk/index.md)
