feat: refactor ProductVariantController and requests to use scoped product handling and update route parameters

This commit is contained in:
2026-06-29 14:51:36 -03:00
parent a7372f9d08
commit edcb73f748
4 changed files with 41 additions and 55 deletions

View File

@@ -18,20 +18,16 @@ class StoreProductVariantRequest extends FormRequest
*/
public function rules(): array
{
/** @var \App\Domains\Catalog\Models\Product|null $product */
$product = $this->route('producto');
return [
'producto_id' => [
'required',
'integer',
Rule::exists('productos', 'id')->where(
fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
),
],
'slug' => [
'nullable',
'string',
'max:255',
Rule::unique('productos_variantes', 'slug')->where(
fn ($query) => $query->where('producto_id', $this->input('producto_id'))
fn ($query) => $query->where('producto_id', $product?->id)
),
],
'nombre' => ['nullable', 'string', 'max:255'],
@@ -47,7 +43,7 @@ class StoreProductVariantRequest extends FormRequest
fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
),
function ($attribute, $value, $fail) {
$productId = $this->input('producto_id');
$productId = $this->route('producto')?->id;
if ($productId) {
$exists = \Illuminate\Support\Facades\DB::table('products_attributes')
->where('product_id', $productId)

View File

@@ -21,22 +21,17 @@ class UpdateProductVariantRequest extends FormRequest
{
/** @var ProductVariant|null $productVariant */
$productVariant = $this->route('productVariant');
/** @var \App\Domains\Catalog\Models\Product|null $product */
$product = $this->route('producto');
return [
'producto_id' => [
'required',
'integer',
Rule::exists('productos', 'id')->where(
fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
),
],
'slug' => [
'nullable',
'string',
'max:255',
Rule::unique('productos_variantes', 'slug')
->ignore($productVariant?->id)
->where(fn ($query) => $query->where('producto_id', $this->input('producto_id'))),
->where(fn ($query) => $query->where('producto_id', $product?->id)),
],
'nombre' => ['nullable', 'string', 'max:255'],
'stock' => ['sometimes', 'integer', 'min:0'],
@@ -51,7 +46,7 @@ class UpdateProductVariantRequest extends FormRequest
fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
),
function ($attribute, $value, $fail) {
$productId = $this->input('producto_id');
$productId = $this->route('producto')?->id;
if ($productId) {
$exists = \Illuminate\Support\Facades\DB::table('products_attributes')
->where('product_id', $productId)