Mohamm6d's avatar

Looking for a package to limit user based on plan

hello dear supportive all, I am looking for a package that allows me to limit users to create models based on role. I can implement something with Laravel policies, but it would be better to have something work with Spatie permission package or even with laravel itself.

What I mean is if a user is on a Basic plan then they can create 5 posts & 10 contacts, for premium 10 posts & 20 contacts, and so on.

your recommendations are appreciated

0 likes
7 replies
garrettmassey's avatar

Check out Laravel Permissions from Spatie. That package lets you define permissions and then group permissions into roles and give permissions and roles to any model in your system. So you can have a permission that says $user->can('create models')

Alternatively, instead of creating a bunch of different permissions and roles, you can simply create a table that stores the limits by plan in rows like this:

id		plan	post_limit	contact_limit
1		basic	5			5
2		premium	10			10
...

and then link that to a user on the users table:

id		name		plan_id
1		John Doe	1

so that in your User model you can check to see what plan they have, and you can write custom methods like postLimitReached

public function postLimitReached(): bool
{
    return $this->posts()->count() < $this->plan->post_limit;
}

so that in your PostController you can check if a user is allowed to create a new post:

public function create()
{
	$user = Auth::user();
	if($user->postLimitReached()) {
		//forbidden
		//but you can handle this however
		abort(403);
	} else {
		//show the form to create a new post
	}
}
2 likes
Mohamm6d's avatar

@garrettmassey thanks for your reply and for providing this. I had something same in my mind but was looking for an existing package, that maybe could save my time, otherwise, that will be my solution. thanks again

1 like
garrettmassey's avatar

@Mohamm6d Honestly I use Spatie's Laravel Permissions package for almost all of my projects, and even then it feels like it would be overkill for what you're trying to achieve. A simple table storing values for the limits on a per-plan basis is probably the easiest way to go, and it gives you the flexibility of being able to edit those fields using Laravel CRUD methods like any other model.

Hopefully you can make something work! I think that a dedicated package might be overkill for this particular feature though.

2 likes
flssol's avatar

@Mohamm6d take a look at Laravel RBAC which is built on top on Spatie permissions.

Ultimately i think it is your application that should drive it, so not sure what kind of package may guess policy logic for you.

Snapey's avatar

your problem is not permissions, it is account limits, for which there are two solutions

  1. give the user credits for each type and then decrement them each time they create an entry of that type

  2. count how many of that type they have each time they try to add another and see if they reached their limit

personally i would use credits because it's easier to implement top up and bonus credits

you will note neither option has anything to do with permissions

2 likes
Merklin's avatar

A bit old but very useful. However, if we use what @garrettmassey proposed and we want to bill the user for these plans what is the best way? Use Laravel Cashier or else?

nabilhassen's avatar

I have a package that aims specifically at solving the OP's question. Feedback are welcome!

github.com/nabilhassen/laravel-usage-limiter

1 like

Please or to participate in this conversation.