niclasleonbock / eloquent-activatable
Creating (de-) activatable Eloquent Models made easy.
Installs: 208
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 1
Forks: 1
Open Issues: 1
pkg:composer/niclasleonbock/eloquent-activatable
Requires
- php: >=5.6.0
- laravel/framework: ~5.4
Requires (Dev)
- nesbot/carbon: *
- orchestra/database: ~3.0
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~5.7
This package is not auto-updated.
Last update: 2021-02-28 08:45:36 UTC
README
Creating (de-) activatable Eloquent Models made easy.
Installation
First, you'll need to add the package to your composer.json and run composer update.
{
"require": {
"niclasleonbock/eloquent-activatable": "~5.0"
}
}
Please require version 4.0 when using with Laravel 4.x.
Now, simply add a datetime column called activated_at to your table and use the ActivatableTrait (niclasleonbock\Eloquent\ActivatableTrait) in your Eloquent model.
Migration
<?php $table->datetime('activated_at')->nullable();
Your Model
<?php use niclasleonbock\Eloquent\ActivatableTrait; class Topic extends Eloquent { use ActivatableTrait; // ... }
And you're done!
Use
withDeactivated()
By default all database queries will be filtered so that only activated data sets are shown. To also show deactivated data sets you may use the withDeactivated method on the query builder.
<?php $allTopics = Topic::withDeactivated()->get();
onlyDeactivated()
To get only deactivated data sets use the onlyDeactivated method.
<?php $onlyDeactivatedTopics = Topic::onlyDeactivated()->get();
activated()
To check whether a data set is deactivated you may use the activated method.
<?php echo 'My topic is ' . ($topic->activated() ? 'activated' : 'deactivated');
activate()
To activate a data set use the activate method.
<?php $topic->activate(); $topic->save(); echo 'My topic is now ' . ($topic->activated() ? 'activated' : 'deactivated');
deactivate()
To deactivate a data set use the deactivate method.
<?php $topic->deactivate(); $topic->save(); echo 'My topic is now ' . ($topic->activated() ? 'activated' : 'deactivated');
Customization
Sometimes the column name activated_at may not fit even though the functionality does. To change the name you can easily override the protected $activatedAtColumn variable or the public getActivatedAtColumn method.
protected $activatedAtColumn = 'my_column_name'; // or public getActivatedAtColumn() { return 'my_column_name'; }