think.studio / laravel-periodic-notice
通过通知批量发送周期性系列内容给用户。
1.4.0
2023-07-11 15:17 UTC
Requires
- php: ^8.1
- illuminate/support: ^9.0|^10.0
- think.studio/laravel-json-field-cast: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.20
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.13
README
通过通知批量发送周期性系列内容给用户。
安装
通过composer安装包
composer require think.studio/laravel-periodic-notice
您可以使用以下命令发布资产文件
php artisan vendor:publish --provider="PeriodicNotice\ServiceProvider" --tag="config" php artisan vendor:publish --provider="PeriodicNotice\ServiceProvider" --tag="lang"
要禁用默认迁移,请将以下代码添加到app服务提供者中
\PeriodicNotice\PeriodicNoticeManager::ignoreMigrations()
用法
use PeriodicNotice\Concerns\HasPeriodicNotice; use PeriodicNotice\Contracts\NotificationReceiver; use PeriodicNotice\PeriodicNoticeDirector; class User extends \Illuminate\Foundation\Auth\User implements NotificationReceiver { use Notifiable; use HasPeriodicNotice; public function periodicNoticeDirector(string $group = 'default'): PeriodicNoticeDirector { $dayInSeconds = 60 * 60 * 24; return PeriodicNoticeDirector::defaults($group) ->usePeriodType($this->periodic_notification_type) ->usePeriodTypesMap([ 'every_day' => round($dayInSeconds), 'every_week' => round($dayInSeconds * 7), ]) ->useQueryToGetEntries(Post::class); } public function scopeWithNotificationPeriodType(Builder $query, string $type, string $group = 'default') { $query->where('periodic_notification_type', '=', $type); } }
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use PeriodicNotice\Concerns\InPeriodicNotice; use PeriodicNotice\Contracts\SendableEntity; use PeriodicNotice\Tests\Fixtures\Factories\PostFactory; class Post extends Model implements SendableEntity { use InPeriodicNotice; public function scopeReleasedAfter(Builder $query, \DateTimeInterface|string $dateTime, string $group) { $query->where('published_at', '>=', $dateTime); } }
手动调用
php artisan periodic-notice:send:batch every_day \\App\\Models\\User # or use morph alias php artisan periodic-notice:send:batch every_day user # use custom group php artisan periodic-notice:send:batch every_day user -G custom_group
更合适的方式是使用cron计划任务
$schedule->command('periodic-notice:send:batch every_day user') ->dailyAt('18:00');