Hi all,
I am currently working on a subscription based application and curious to know the best way to deal with serving features to uses that are subscribe to different plans.
This is how I am currently doing it but I am sure there are more efficient ways:
In my 'Plan' model I have the following function:
//Function to check for subscription plan features
public static function checkSubscription($route)
{
$user = Sentry::getUser();
$account = $user->account()->first();
if($account->plan->id == 1) //Free Plan
{
switch($route) {
case 'apiaries':
//Fetch all apiaries for this account
$apiaries = Apiary::where('account_id','=',$account->id);
if($apiaries->count() >= 1){
Session::put('plan-message', 'You have reached your maximum number of Apiaries. To add more please upgrade your plan.');
return true;
}else{
Session::forget('plan-message');
return false;
}
break;
case 'harvests':
//Fetch all harvests for this account
$harvests = Harvest::where('account_id','=',$account->id);
if($harvests->count() >= 1){
Session::put('plan-message', 'You have reached your maximum number of Harvests. To add more please upgrade your plan.');
return true;
}else{
Session::forget('plan-message');
return false;
}
break;
case 'tasks':
//Fetch all tasks for this account
$tasks = Task::where('account_id','=',$account->id);
if($tasks->count() >= 1){
Session::put('plan-message', 'You have reached your maximum number of Tasks. To add more please upgrade your plan.');
return true;
}else{
Session::forget('plan-message');
return false;
}
break;
default:
Session::forget('plan-message');
break;
}
}
Session::forget('plan-message');
}
This is then called from the base controller:
//Check subscription plan features
//Return bool
$this->hasAccess = Plan::checkSubscription(Request::segment(1));
I then use the hasAccess variable in my view files to hide and show buttons etc
Thoughts?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community