Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get a plan feature by its code #7

Merged
merged 1 commit into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}