scito / eloquent-webhooks
基于 eloquent 事件触发 webhooks
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
无需配置即可在模型事件上触发 HTTP 请求。
安装
安装此包。
composer require scito/eloquent-webhooks
运行迁移以创建 eloquent_webhooks 表。
php artisan migrate
如果您没有自动发现,请在 config/app.php
中添加服务提供者。
'providers' = [
Scito\EloquentWebhooks\Providers\WebhookServiceProvider::class,
// ...
];
配置
在您的模型中添加 Webhooks 特性。默认情况下,您的模型的所有已发布属性将在评估 webhook url 中的短代码时可用。
use Illuminate\Database\Eloquent\Model;
use Scito\EloquentWebhooks\Traits\Webhooks;
class Post extends Model
{
use Webhooks;
// ...
}
webhook 中的短代码将与 $model->toArray()
进行比较。
您可以通过在模型中添加 webhookData()
来自定义要评估的数据。
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,
];
}
// ...
}
创建 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}',
]);
您还可以触发 POST 请求,webhookData()
和 $model->toArray()
将作为请求负载发送。
use Scito\EloquentWebhooks\Models\EloquentWebhook;
EloquentWebhook::create([
'model' => Post::class,
'event' => 'created',
'status' => 'active',
'method' => 'post',
'link' => 'https://example.com',
]);