PHP code example of contrainteractive / content-scheduler

1. Go to this page and download the library: Download contrainteractive/content-scheduler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contrainteractive / content-scheduler example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use ContraInteractive\ContentScheduler\Traits\Schedulable;
use ContraInteractive\ContentScheduler\Traits\HasScheduling;

class Post extends Model
{
    use Schedulable;
    use HasScheduling;

    // ...
}

$model = AnyModel::find(1);

Scheduler::schedulePublish($model, '2027-01-23')
    // Optionally, you can set the scheduled unpublish date
    ->scheduleUnpublish($model, '2028-01-25');


use Illuminate\Console\Scheduling\Schedule;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })
     ->withSchedule(callback: function (Schedule $schedule) {
         $schedule->command('schedules:process')
         ->everyMinute();
     })
    ->create();

//Publish Immediately
Scheduler::publish($model);
$model->isPublished()  // true.

Scheduler::unpublish($model);
$model->isPublished()  // false.
	
//Schedule Future Publish
Scheduler::schedulePublish($model, '2025-02-01');
$model->isScheduled() // true.

//Schedule Future Unpublish
Scheduler::scheduleUnpublish($model, '2025-02-10');
$model->isScheduledForFutureUnpublish() // true.

//  ContraInteractive\ContentScheduler\Traits\Schedulable::class

Post::whereScheduleStatus('SCHEDULED')->get();

Post::published()->get();

Post::scheduled()->get();

Post::unpublished()->get();
bash
php artisan vendor:publish --provider="ContraInteractive\ContentScheduler\Providers\ContentSchedulerServiceProvider" --tag="migrations"
php artisan migrate