PHP code example of stfn / laravel-pending-updates

1. Go to this page and download the library: Download stfn/laravel-pending-updates 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/ */

    

stfn / laravel-pending-updates example snippets


$news = News::find(1);

$news->postpone()
    ->startFrom('2023-01-01 00:00:00')
    ->keepForHours(24)
    ->update(['is_active' => true]);

return [
    // Maximum postpone in days.
    'max_postpone_days' => 10,

    // The model uses to store pending updates.
    'model' => \Stfn\PendingUpdates\Models\PendingUpdate::class,
];


// app/Console/Kernel.php
use Stfn\PendingUpdates\Commands\CheckPendingUpdates;

protected function schedule(Schedule $schedule)
{
   $schedule->command(CheckPendingUpdates::class)->everyMinute();
}

use Illuminate\Database\Eloquent\Model;
use Stfn\PendingUpdates\Models\Concerns\HasPendingUpdates;

class Ticket extends Model
{
    use HasPendingUpdates;
}

$ticket = Ticket::find(1);

// Update ticket price to 200 and keep it updated for 60 minutes.
$ticket->postpone()
    ->keepForMinutes(60)
    ->update(['price' => 200]);
    
// Update ticket price to 200 and keep it updated for 12 hours.
$ticket->postpone()
    ->keepForHours(12)
    ->update(['price' => 200]);

// Update ticket price to 200 and keep it updated for 3 days.
$ticket->postpone()
    ->keepForDays(3)
    ->update(['price' => 200]);

$ticket = Ticket::find(1);

// Update ticket price to 200 after 60 minutes from now and keep it like that for unlimited time.
$ticket->postpone()
    ->delayForMinutes(60)
    ->update(['price' => 200]);

// Update ticket price to 200 after 12 hours from now and keep it like that for unlimited time.
$ticket->postpone()
    ->delayForHours(12)
    ->update(['price' => 200]);

// Update ticket price to 200 after 3 days from now and keep it like that for unlimited time.
$ticket->postpone()
    ->delayForDays(3)
    ->update(['price' => 200]);

$product = Product::find(1);

// Update product to be unavailable from 1st January.
$product->postpone()
    ->startFrom("2023-01-01 00:00:00")
    ->update(['is_available' => false]);

// Update product to be unavailable until 4th January.
$product->postpone()
    ->revertAt("2023-04-01 00:00:00")
    ->update(['is_available' => false]);

// Update product to be unavailable from 1st January to 4th January.
$product->postpone()
    ->startFrom("2023-01-01 00:00:00")
    ->revertAt("2023-04-01 00:00:00")
    ->update(['is_available' => false]);

$product = Product::find(1);

// Update product to be unavailable from 1st January and keep that state for 1 day.
$product->postpone()
    ->startFrom("2023-01-01 00:00:00")
    ->keepForDays(1)
    ->update(['is_available' => false]);

// Update product to became unavailable after 60 minutes from now and keep that state until 4th January.
$product->postpone()
    ->delayForMinutes(60)
    ->revertAt("2023-04-01 00:00:00")
    ->update(['price' => 200]);

use Illuminate\Database\Eloquent\Model;
use Stfn\PendingUpdates\Models\Concerns\HasPendingUpdates;

class Ticket extends Model
{
    use HasPendingUpdates;
    
    public function allowedPendingAttributes()
    {
        return ['price'];
    }
}

use Illuminate\Database\Eloquent\Model;
use Stfn\PendingUpdates\Models\PendingUpdate;

class CustomPendingUpdate extends PendingUpdate
{    
    public function updateCannotBeApplied($exception, $model)
    {
        // Your custom logic here
    }
}
bash
php artisan vendor:publish --tag="pending-updates-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="pending-updates-config"