Skip to content

Commit

Permalink
Get a plan feature by its code
Browse files Browse the repository at this point in the history
  • Loading branch information
Konafets committed Apr 19, 2017
1 parent 3b98ef7 commit 19d7a08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ $plan->features()->saveMany([
]);
```

### Get the value of Feature

Say you want to show the value of the feature _pictures_per_listing_ from above. You can use `getFeatureByCode()`.

```php
$amountOfPictures = $plan->getFeatureByCode('pictures_per_listing')->value
```

### Creating subscriptions

You can subscribe a user to a plan by using the `newSubscription()` function available in the `PlanSubscriber` trait. First, retrieve an instance of your subscriber model, which typically will be your user model and an instance of the plan your user is subscribing to. Once you have retrieved the model instance, you may use the `newSubscription` method to create the model's subscription.
Expand Down
21 changes: 21 additions & 0 deletions src/LaraPlans/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Gerardojbaez\LaraPlans\Models;

use Gerardojbaez\LaraPlans\Exceptions\InvalidPlanFeatureException;
use Gerardojbaez\LaraPlans\Period;
use Illuminate\Database\Eloquent\Model;
use Gerardojbaez\LaraPlans\Contracts\PlanInterface;
Expand Down Expand Up @@ -112,4 +113,24 @@ public function hasTrial()
{
return (is_numeric($this->trial_period_days) and $this->trial_period_days > 0);
}

/**
* Returns the demanded feature
*
* @param String $code
* @return PlanFeature
* @throws InvalidPlanFeatureException
*/
public function getFeatureByCode($code)
{
$feature = $this->features()->getEager()->first(function($item) use ($code) {
return $item->code === $code;
});

if (is_null($feature)) {
throw new InvalidPlanFeatureException($code);
}

return $feature;
}
}

0 comments on commit 19d7a08

Please sign in to comment.