think.studio / laravel-periodic-notice
Send your periodical series to user as batch using notifications.
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
Send your periodical series to user as batch using notifications.
Installation
Install the package via composer:
composer require think.studio/laravel-periodic-notice
You can publish the assets file with:
php artisan vendor:publish --provider="PeriodicNotice\ServiceProvider" --tag="config" php artisan vendor:publish --provider="PeriodicNotice\ServiceProvider" --tag="lang"
To disable default migrations add this code to app service provider:
\PeriodicNotice\PeriodicNoticeManager::ignoreMigrations()
Usage
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); } }
Manual call
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
More appropriate way is using cron schedule
$schedule->command('periodic-notice:send:batch every_day user') ->dailyAt('18:00');