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

Add methods to plan to retrieve informations about the features #3

Closed
Konafets opened this issue Apr 17, 2017 · 2 comments
Closed

Add methods to plan to retrieve informations about the features #3

Konafets opened this issue Apr 17, 2017 · 2 comments

Comments

@Konafets
Copy link
Contributor

Konafets commented Apr 17, 2017

Imagine you have a list of plans and want to display the name of the plan, the price and the amount of pictures a user can upload pro plan.

Title Price Amount of pictures
Basic 0.00 10
Pro 10.00 20

The Features were created like this:

$planBasic = Plan::create([
    'name' => 'Basic',
    'price' => 0.00,
]);

$planBasic->features()->save(
    new PlanFeature(['code' => 'amount_of_pics', 'value' => 10, 'sort_order' => 1])
);
$planPro = Plan::create([
    'name' => 'Pro',
    'price' => 10.00,
]);

$planPro->features()->save(
    new PlanFeature(['code' => 'amount_of_pics', 'value' => 20, 'sort_order' => 2])
);

It would be nice to have a method at the plan model which returns the value of a feature found by its code. Here is my implementation and usage:

    /**
     * @return integer
     */
    public function getPicturesAttribute() : int 
    {
        return (int) $this->getFeatureByCode('amount_of_pics')->value;
    }

    /**
     * @param String $code
     * @return PlanFeature
     */
    public function getFeatureByCode(String $code) : PlanFeature
    {
        return $this->features()->getEager()->first(function($item) use ($code) {
            return $item->code === $code;
        });
    }

If you interested in this, I'll open a PR.

@gerardojbaez
Copy link
Owner

A method like your getFeatureByCode will be helpful. I don't think that feature specific getters (like your getPicturesAttribute should be implemented to Laraplans. Plan's features vary for each application. :)

@Konafets
Copy link
Contributor Author

The getPicturesAttribute() was just an illustration of how the other method might be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants