feat: product pricing plans
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
a3894d2f37
commit
768c814762
@@ -3,12 +3,14 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\PricingPlan;
|
||||
use App\Models\Product;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Laravel\Pennant\Feature;
|
||||
|
||||
class ProductController extends Controller
|
||||
@@ -227,6 +229,11 @@ class ProductController extends Controller
|
||||
'screenshots.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'remove_screenshots' => 'nullable|array',
|
||||
'remove_screenshots.*' => 'integer',
|
||||
'plans' => 'nullable|array',
|
||||
'plans.*.name' => 'required|string|max:100',
|
||||
'plans.*.price' => 'required|numeric|min:0|max:99999',
|
||||
'plans.*.period' => ['required', Rule::in(PricingPlan::PERIODS)],
|
||||
'plans.*.description' => 'nullable|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -235,6 +242,18 @@ class ProductController extends Controller
|
||||
*/
|
||||
protected function syncDetails(Request $request, Product $product): void
|
||||
{
|
||||
// The submitted plan list replaces all existing plans
|
||||
$product->pricingPlans()->delete();
|
||||
foreach (array_values((array) $request->input('plans', [])) as $i => $plan) {
|
||||
$product->pricingPlans()->create([
|
||||
'name' => $plan['name'],
|
||||
'price' => $plan['price'],
|
||||
'period' => $plan['period'],
|
||||
'description' => $plan['description'] ?? null,
|
||||
'position' => $i,
|
||||
]);
|
||||
}
|
||||
|
||||
// Only rows belonging to this product may be removed
|
||||
$ids = array_filter((array) $request->input('remove_screenshots', []));
|
||||
if ($ids) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PricingPlan extends Model
|
||||
{
|
||||
public const PERIODS = ['monthly', 'yearly', 'one-time'];
|
||||
|
||||
protected $fillable = ['product_id', 'name', 'price', 'period', 'description', 'position'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['price' => 'decimal:2'];
|
||||
}
|
||||
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Price formatted for display, e.g. "€9.99" ("€0" for free plans).
|
||||
*/
|
||||
public function formattedPrice(): string
|
||||
{
|
||||
return (float) $this->price == 0 ? '€0' : '€'.number_format((float) $this->price, 2);
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,14 @@ class Product extends Model
|
||||
return $this->hasMany(ProductScreenshot::class)->orderBy('position')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product's pricing plans in display order.
|
||||
*/
|
||||
public function pricingPlans(): HasMany
|
||||
{
|
||||
return $this->hasMany(PricingPlan::class)->orderBy('position')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user that owns the product.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user