Skip to content

ProductSpecification Model

Defines product specification templates and metadata for flexible product attributes.

Overview

The ProductSpecification model defines the structure and validation rules for product specifications. It serves as a template system that allows for flexible, dynamic product attributes beyond the standard fields.

Attributes

AttributeTypeDescription
idintPrimary key
namestringSpecification name
keystringUnique key identifier
descriptiontextSpecification description
data_typestringData type (string, integer, boolean, etc.)
is_requiredbooleanRequired field flag
is_searchablebooleanSearchable field flag
validation_rulesjsonJSON validation rules
optionsjsonJSON available options
sort_orderintDisplay order
is_activebooleanActive status flag
created_attimestampCreation timestamp
updated_attimestampLast update timestamp

Relationships

Has Many

  • hasMany(ProductSpecificationValue) - Specification values

Belongs To Many

  • belongsToMany(Product) - Products with this specification

Key Features

Flexible Data Types

  • Support for various data types (string, integer, boolean, etc.)
  • Custom validation rules per specification
  • Option-based specifications (dropdowns, checkboxes)

Search Integration

  • Searchable specifications for product filtering
  • Indexed specification values
  • Fast specification-based queries

Validation System

  • JSON-based validation rules
  • Required field enforcement
  • Custom validation logic

Organization

  • Sort order for display organization
  • Active/inactive status management
  • Hierarchical specification grouping

Usage Examples

Creating a Product Specification

php
$specification = ProductSpecification::create([
    'name' => 'Dosage Strength',
    'key' => 'dosage_strength',
    'description' => 'Strength of the medication',
    'data_type' => 'string',
    'is_required' => true,
    'is_searchable' => true,
    'validation_rules' => [
        'required' => true,
        'in' => ['100mg', '200mg', '400mg', '600mg', '800mg']
    ],
    'options' => [
        '100mg' => '100mg',
        '200mg' => '200mg',
        '400mg' => '400mg',
        '600mg' => '600mg',
        '800mg' => '800mg'
    ],
    'sort_order' => 1,
    'is_active' => true
]);

Getting Associated Data

php
// Get specification values
$values = $specification->specificationValues;

// Get products with this specification
$products = $specification->products;

// Get active values
$activeValues = $specification->specificationValues()
    ->whereHas('product', function ($query) {
        $query->where('is_available', true);
    })->get();

Searching Specifications

php
// Get active specifications
$activeSpecs = ProductSpecification::active()->get();

// Get searchable specifications
$searchableSpecs = ProductSpecification::searchable()->get();

// Get specifications by data type
$stringSpecs = ProductSpecification::where('data_type', 'string')->get();

Scopes

Active Specifications

php
// Get only active specifications
ProductSpecification::active()->get();

Searchable Specifications

php
// Get only searchable specifications
ProductSpecification::searchable()->get();

Required Specifications

php
// Get only required specifications
ProductSpecification::where('is_required', true)->get();

Validation Rules

Specifications can have custom validation rules stored as JSON:

php
// Example validation rules
$validationRules = [
    'required' => true,
    'min' => 1,
    'max' => 100,
    'in' => ['option1', 'option2', 'option3'],
    'regex' => '/^[A-Za-z0-9\s]+$/'
];

$specification->validation_rules = $validationRules;
$specification->save();

Options Management

For specifications with predefined options (like dropdowns):

php
// Set available options
$options = [
    'small' => 'Small Size',
    'medium' => 'Medium Size',
    'large' => 'Large Size'
];

$specification->options = $options;
$specification->save();

// Get options
$availableOptions = $specification->options;

Sort Order

Specifications can be ordered for display:

php
// Get specifications in order
$orderedSpecs = ProductSpecification::active()
    ->orderBy('sort_order')
    ->orderBy('name')
    ->get();