scito / eloquent-webhooks
Fire webhooks based on eloquent events
1.01
2022-12-23 12:23 UTC
Requires
- ext-json: *
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- orchestra/testbench: ^6.25
- phpunit/phpunit: ^9.5
README
Dispatch http requests on model events with zero configuration.
Installation
Install the package.
composer require scito/eloquent-webhooks
Run the migration to create eloquent_webhooks table.
php artisan migrate
If you don't have auto discovery add the service provider in config/app.php
.
'providers' = [
Scito\EloquentWebhooks\Providers\WebhookServiceProvider::class,
// ...
];
Configuration
Add the Webhooks trait in your model. Out of the box all published properties of your model will be available during the evaluation of the shortcodes in your webhook url.
use Illuminate\Database\Eloquent\Model;
use Scito\EloquentWebhooks\Traits\Webhooks;
class Post extends Model
{
use Webhooks;
// ...
}
The shotcodes in a webhook will be evaluated against $model->toArray()
.
You can customize the data to evaluate by adding webhookData()
in your model.
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Scito\EloquentWebhooks\Models\EloquentWebhook;
use Scito\EloquentWebhooks\Traits\Webhooks;
class Post extends Model
{
use Webhooks;
public function author(): BelongsTo
{
return $this->belongsTo(Author::class);
}
public function webhookData(EloquentWebhook $webhook): array
{
return [
'title' => $this->name,
'author' => $this->author->name,
];
}
// ...
}
Creating webhooks
use Scito\EloquentWebhooks\Models\EloquentWebhook;
EloquentWebhook::create([
'model' => Post::class,
'event' => 'created',
'status' => 'active',
'method' => 'get',
'link' => 'https://example.com?name={title}&author={author}',
]);
You can also dispatch post requests, webhookData()
/ $model->toArray()
will be sent as request payload.
use Scito\EloquentWebhooks\Models\EloquentWebhook;
EloquentWebhook::create([
'model' => Post::class,
'event' => 'created',
'status' => 'active',
'method' => 'post',
'link' => 'https://example.com',
]);