neelkanthk / laravel-schedulable
一个用于在 Eloquent 模型中添加调度能力的 Laravel 扩展包。
Requires
- php: >=7.2
- laravel/framework: >=6.0
Requires (Dev)
- doctrine/dbal: ^4.0@dev
- orchestra/testbench: ^6.0|^7.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-08-29 05:43:32 UTC
README
Laravel Schedulable 
优雅地安排和取消安排任何 Eloquent 模型,无需使用 cron 作业。
显著特性
-
使用模型中的
Schedulable
特性将任何 Eloquent 模型转换为可安排模型。 -
安排模型在未来某个时间点执行,它们将在指定的日期和时间出现在查询结果中。
-
在任何时间重新安排和取消安排使用简单的方法。
-
通过包提供的 自定义模型事件 钩入模型的生命周期。
-
覆盖默认列名并使用您自己的自定义列名。
以下是一些此包可能有用的示例用例
-
一个博客类型的应用程序,允许博客作者将文章安排在未来的日期和时间发布。
-
一个电子商务网站,管理员可以从管理面板随时添加库存项,但可以将它们安排在特定的日期和时间对客户可用。
最低要求
- Laravel 6.0
- PHP 7.2
安装
composer require neelkanthk/laravel-schedulable
用法
1. 使用包的 scheduleAt();
方法创建迁移,在任意表中添加 schedule_at
列。
注意:如果您想使用其他列名,则可以使用如以下示例所示的 $table->timestamp('column_name');
方法。
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddScheduleAtColumnInPosts extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function (Blueprint $table) { $table->scheduleAt(); //Using default schedule_at column //or $table->timestamp('publish_at', 0)->nullable(); //Using custom column name }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function (Blueprint $table) { $table->dropColumn('schedule_at'); //Using default schedule_at column //or $table->dropColumn('publish_at'); //Using custom column name }); } }
2. 在任何模型中使用 Neelkanth\Laravel\Schedulable\Traits\Schedulable
特性。
注意:如果您在迁移中使用了自定义列名,则必须在模型中指定该列,如下所示。
use Illuminate\Database\Eloquent\Model; use Neelkanth\Laravel\Schedulable\Traits\Schedulable; class Post extends Model { use Schedulable; const SCHEDULE_AT = "publish_at"; //Specify the custom column name }
用法
1. 安排模型
$scheduleAt = Carbon::now()->addDays(10); //Carbon is just an example. You can pass any object which is implementing DateTimeInterface. $post = new App\Post(); //Add values to other attributes $post->scheduleWithoutSaving($scheduleAt); // Modifies the schedule_at attribute and returns the current model object without saving it. $post->schedule($scheduleAt); //Saves the model in the database and returns boolean true or false
2. 取消安排模型
$post = App\Post::find(1); $post->unscheduleWithoutSaving(); // Modifies the schedule_at attribute and returns the current model object without saving it. $post->unschedule(); //Saves the model in the database and returns boolean true or false
3. 事件和观察者
该包提供了四个模型事件和观察者方法,开发人员可以使用它们来钩入模型的生命周期。
schedule()
方法在保存模型之前触发 scheduling
事件,在保存模型后触发 scheduled
事件。
unschedule()
方法在保存模型之前触发 unscheduling
事件,在保存模型后触发 unscheduled
事件。
以下是在观察者类中捕获上述事件的方法
namespace App\Observers; use App\Post; class PostObserver { public function scheduling(Post $post) { // } public function scheduled(Post $post) { // } public function unscheduling(Post $post) { // } public function unscheduled(Post $post) { // } }
4. 使用查询获取数据
以下示例将以下帖子表作为参考
以下示例假设当前时间戳为 2020-10-18 00:00:00。
1. 默认
默认情况下,所有那些模型都会被检索,其中 schedule_at
列的值为 NULL
或小于或等于当前时间戳的戳记。
因此,以下 Eloquent 查询
$posts = App\Post::get();
将返回 Toy Story 1 和 Toy Story 2
2. 获取除常规模型外的安排模型
要获取除常规模型外的安排模型,请使用 withScheduled()
方法。
$posts = App\Post::withScheduled()->get();
上述查询将返回上述表中的所有四行。
3. 仅获取安排模型,不获取常规模型
要仅获取安排模型,请使用 onlyScheduled()
方法。
$posts = App\Post::onlyScheduled()->get();
上述查询将返回 Toy Story 3 和 Terminator 2。
4. 不应用 Schedulable
提供的任何功能。
在某些情况下,你可能根本不想应用 Schedulable
特性。在这些情况下,请在你的查询中使用 withoutGlobalScope()
方法。
use Neelkanth\Laravel\Schedulable\Scopes\SchedulableScope; $posts = App\Post::withoutGlobalScope(SchedulableScope::class)->get();
一个通用用例示例。
// routes/web.php
use Illuminate\Support\Facades\Route;
use Neelkanth\Laravel\Schedulable\Scopes\SchedulableScope;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/schedule/post', function () {
$post = new App\Post();
$post->title = "My scheduled post";
$scheduleAt = Carbon\Carbon::now()->addDays(10);
$post->schedule($scheduleAt);
return $post; //The scheduled post's ID is 1
});
Route::get('/unschedule/post', function () {
// To unschedule a post you have to fetch the scheduled post first.
// But becuase the Schedulable trait is used in App\Post model it will not a fetch a post whose schedule_at column value is in future.
$post = App\Post::find(1); //This will return null for a scheduled post whose id is 1.
//To retreive a scheduled post you can use any of the two methods given below.
$post = App\Post::withScheduled()->find(1); //1. Using withScheduled() [Recommended]
$post = App\Post::withoutGlobalScope(SchedulableScope::class)->find(1); //2. Using withoutGlobalScope()
$post->unschedule();
});
贡献
欢迎提交拉取请求。对于重大更改,请先打开一个问题来讨论你想要进行的更改。
安全
如果你发现任何与安全相关的问题,请通过电子邮件 me.neelkanth@gmail.com 联系,而不是使用问题跟踪器。