PHP code example of bernskiold / laravel-activatable

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

    

bernskiold / laravel-activatable example snippets


$product->activate();
$product->deactivate();

Product::active()->get();   // only the live ones
Product::inactive()->get(); // only the switched-off ones

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->activatable();   // adds an `is_active` boolean, default true
    $table->timestamps();
});

$table->activatable()->comment('Whether the product is purchasable.');

use Bernskiold\LaravelActivatable\Concerns\Activatable;

class Product extends Model
{
    use Activatable;
}

$product->isActive();    // bool
$product->isInactive();  // bool

$product->activate();      // sets active, dispatches ModelActivated (on change)
$product->deactivate();    // sets inactive, dispatches ModelDeactivated (on change)
$product->toggleActive();  // flips it

Product::active()->get();      // only active
Product::inactive()->get();    // only inactive
Product::active(false)->get(); // inverted — same as inactive()

$product->activateQuietly();
$product->deactivateQuietly();

use Bernskiold\LaravelActivatable\Events\ModelDeactivated;

Event::listen(ModelDeactivated::class, function (ModelDeactivated $event) {
    // $event->model was just switched off
});

class Product extends Model
{
    use Activatable;

    protected bool $tracksInactivatedAt = true;
}

$table->activatable();
$table->inactivatedAt();   // adds a nullable `inactivated_at` timestamp

use Bernskiold\LaravelActivatable\Concerns\ActivatableFactory;

class ProductFactory extends Factory
{
    use ActivatableFactory;
}

Product::factory()->inactive()->create();

class FeatureFlag extends Model
{
    use Activatable;

    public const ACTIVE_COLUMN = 'is_enabled';
}

use Bernskiold\LaravelActivatable\Contracts\Activatable;

function publish(Activatable $model): void
{
    $model->activate();
}
bash
php artisan vendor:publish --tag=activatable-config