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();
// 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();
// 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();