PHP code example of jkbennemann / laravel-features

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

    

jkbennemann / laravel-features example snippets


//..
use Jkbennemann\Features\Models\Traits\HasFeatures;

class User extends Model
{
    use HasFeatures;
    //..
}

use \Jkbennemann\Features\Models\Enums\FeatureStatus;
use \Jkbennemann\Features\Models\Feature;

$feature = Features::create([
    'name' => 'Functionality A',
    'description' => 'This is a new feature', //optional
    'status' => FeatureStatus::ACTIVE //optional, defaults to INACTIVE
]);

$party = Party::create([
    'name' => 'Beta Testers',
    'description' => 'This is a new party', //optional
    'status' => FeatureStatus::ACTIVE //optional, defaults to INACTIVE
]);

$feature->activate();       //activates a feature
$feature->deactivate();     //deactivates a feature

$party->addFeature($feature);       //assigns a feature to a party
$party->removeFeature($feature);    //removes a feature from a party

$active = $user->hasFeature($feature);        //user has the feature which is active
$active = $user->hasFeature('feature-slug');  //you may provide the slug of a feature
$active = $user->hasFeature('feature-slug', false); //provide false ignore the check for active features
$active = $user->hasFeatureThroughParty('feature-slug');    //checks if a feature is granted through a party

$features = $user->allFeatures();   //returns all features
$features = $user->allFeatures(false);   //returns all features without checking the status

$user->giveFeature('feature-slug');     //add specific feature to a user
$user->removeFeature('feature-slug');   //remove specific feature

$user->joinParty('party-slug');     //add a user to a party
$user->addToParty('party-slug');    //add a user to a party
$user->leaveParty('party-slug');    //remove a user from a party

$user->belongsToParty('party-slug');    //checks if the user belongs to the party
$user->inParty('party-slug');           //checks if the user belongs to the party

# Check feature
$user->can('feature-slug');   //allows to check using laravel gates
$user->can('feature-slug', true);    //validates if feature is ACTIVE
$user->can('feature-slug', false);    //ignores status of feature
bash
php artisan migrate
bash
# Create a new feature/party
php artisan feature:add {name} {description?} {--status}
php artisan party:add {name} {description?} {--status}

# List features/parties
php artisan feature:list
php artisan party:list

# Activate a feature/party
php artisan feature:activate {id|slug}
php artisan party:activate {id|slug}

# Deactivate a feature/party
php artisan feature:deactivate {id|slug}
php artisan party:deactivate {id|slug}