John Davidson

php - Laravel Spark - Defining a free plan

0 comments
Message:


I am using Laravel Spark Next and I am trying to create a free plan. My application is charging users "Per seat" (# of projects)


My application, however, will always have one free project included. That means I need to offer two plans: one free and one paid


In my SparkServiceProvider, I have added this:


Spark::billable(User::class)->chargePerSeat('lejemål', function (User $billable) {
$projects = return $billable->projects()->count();

//The first project is always free.
return match ($projects) {
0, 1 => 0,
default => $leases,
};

});

As far as I understand, the "chargePerSeat" method simply calculate the price to charge the user. So in this case, if the user have 0 or 1 project and chooses to subscribe, the price to be paid is zero (as it should be).


However, I am trying to understand how a free plan should work. In the classic Spark, we could define a "free plan" in the configuration.


When users are creating projects, I have added the subscribed middleware as per the documentation:


Route::get('projects/create', CreateProject::class)
->name('projects.create')
->middleware('subscribed');

It seems like this middleware simply checks if there is an active subscription or not. If there is not, it redirects the user to the /billing page. However, if users simply just want to stay on the free plan, he/she should not need to create an active subscription.


How can I provide a "free plan" using Laravel Spark Next?



My Answer:
for premium features, but I want to offer a basic plan for free. How can I define a free plan in Laravel Spark?

To define a free plan in Laravel Spark, you can follow these steps:

1. Define the free plan in your SparkServiceProvider.php file. You can do this by adding the following code to the boot() method:

php
Spark::plan('Free', 'free')
->price(0)
->features([
'Feature 1',
'Feature 2',
'Feature 3',
]);


2. Update your SparkServiceProvider.php file to include the free plan in the available plans. You can do this by adding the following code to the boot() method:

php
Spark::free('Free');


3. Update your subscription blade template to display the free plan as an option for users to select. You can do this by adding the following code to your subscription blade template:

html






4. Update your subscription controller to handle the selection of the free plan. You can do this by adding the following code to your subscription controller:

php
public function subscribe(Request $request)
{
$user = Auth::user();

$user->newSubscription('default', $request->plan)->create();

return redirect()->route('dashboard');
}


By following these steps, you can define a free plan in Laravel Spark and offer it to your users as an option.

Rate this post

3 of 5 based on 5688 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us