PHP code example of alajusticia / laravel-expirable

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

    

alajusticia / laravel-expirable example snippets


use ALajusticia\Expirable\Traits\Expirable;
use Illuminate\Database\Eloquent\Model;

class Subscription extends Model
{
    use Expirable;

use ALajusticia\Expirable\Traits\Expirable;
use Illuminate\Database\Eloquent\Model;

class Subscription extends Model
{
    use Expirable;
    
    const EXPIRES_AT = 'ends_at';

use ALajusticia\Expirable\Traits\Expirable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Subscription extends Model
{
    use Expirable;
    
    // ...

    public static function defaultExpiresAt()
    {
        return Carbon::now()->addMonths(6);
    }

 // Create a new subscription which will expire in six months (using default expiration date)
 $subscription = new Subscription;
 $subscription->save();
 
 // Create a new subscription which will expire in one year (overwrite the default expiration date)
  $subscription = new Subscription;
  $subscription->expiresAt(Carbon::now()->addYear());
  $subscription->save();
 

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->expirable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->dropExpirable();
        });
    }
};

    public function up()
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->expirable('ends_at');
        });
    }

    public function down()
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->dropExpirable('ends_at');
        });
    }

// Get only valid and current subscriptions and exclude expired ones
$subscriptions = Subscription::all();

$user = App\User::find(1);
$subscriptions = $user->subscriptions();

// Get all subscriptions
$subscriptions = Subscription::withExpired()->get();

// Get expired subscriptions
$subscriptions = Subscription::onlyExpired()->get();

// Get unlimited subscriptions
$subscriptions = Subscription::onlyEternal()->get();

// Delete expired models since one year or more
Subscription::expiredSince('1 year')->delete();

$subscription->getExpirationDate(); // Returns a date object

// Create a new subscription valid for one month
$subscription = new Subscription();
$subscription->ends_at = Carbon::now()->addMonth();
$subscription->save();

// Create a new subscription valid for one month
$subscription = Subscription::create([
    'plan' => 'premium',
    'ends_at' => Carbon::now()->addMonth(),
]);

// Create a new subscription valid for one month
$subscription = new Subscription();
$subscription->expiresAt(Carbon::now()->addMonth());
$subscription->save();

// Set multiple subscriptions valid for one year
Subscription::whereKey([1, 2, 3])->expiresAt(Carbon::now()->addYear());

// Create a new subscription valid for one month
$subscription = new Subscription();
$subscription->lifetime('1 month');
$subscription->save();

// Make a model expire
$subscription->expire();

Subscription::expireByKey(1);

Subscription::expireByKey(1, 2, 3);

Subscription::expireByKey([1, 2, 3]);

Subscription::expireByKey(collect([1, 2, 3]));

Subscription::where('plan', 'basic')->expire();

// Reset validity with the default expiration date or set validity for unlimited period
$subscription->revive();

// Set the model to expire in one month
$subscription->revive(Carbon::now()->addMonth());

// Revive by query
Subscription::where('plan', 'plus')->revive();

// Make a model eternal
$subscription->makeEternal();

// Make eternal by query
Subscription::where('plan', 'business')->makeEternal();

$subscription->extendLifetimeBy('1 month')->save();

$subscription->shortenLifetimeBy('3 days')->save();

$subscription->resetExpiration()->save();

if ($subscription->isExpired()) {
    $user->notify(new RenewalProposal($subscription));
}

    'purge' => [
        \App\Models\Subscription::class,
    ],
bash
php artisan vendor:publish --provider="ALajusticia\Expirable\ExpirableServiceProvider"

php artisan expirable:purge

php artisan expirable:purge "App\Models\Subscription" "App\Models\Coupon"

php artisan expirable:purge "App\Models\Subscription" --since="2 months"

php artisan expirable:purge --mode=soft