Skip to main content

Category

Definition and Role: The Category model represents a product category within the application. It is used to manage and store information related to different categories under which products can be organized. This model helps in categorizing products, making it easier for users to filter and find products based on their categories.

Relationships:

  • Children: The Category model has a one-to-many relationship with itself, representing subcategories.
    /** @return HasMany<Category> */
    public function children(): HasMany
    {
    return $this->hasMany(Category::class, 'parent_id');
    }
  • Parent: The Category model has a belongs-to relationship with itself, representing the parent category.
    /** @return BelongsTo<Category,self> */
    public function parent(): BelongsTo
    {
    return $this->belongsTo(Category::class, 'parent_id');
    }
  • Products: The Category model has a many-to-many relationship with the Product model. This means that a category can have multiple products, and a product can belong to multiple categories.
    /** @return BelongsToMany<Product> */
    public function products(): BelongsToMany
    {
    return $this->belongsToMany(Product::class, 'shop_category_product', 'shop_category_id', 'shop_product_id')->withTimestamps();
    }

Traits:

  • HasFactory: Provides factory methods for creating model instances.
  • InteractsWithMedia: Allows the model to interact with media files.
  • LogsActivity: Enables activity logging for the model.

Attributes:

  • table: Specifies the database table name associated with the model.
    protected $table = 'shop_categories';
  • casts: Defines the data types of the model's attributes.
    protected $casts = [
    'is_visible' => 'boolean',
    ];

Activity Log Options:

  • The getActivitylogOptions method returns the default logging options for the model.
    public function getActivitylogOptions(): LogOptions
    {
    return LogOptions::defaults();
    }

Allowed Fields, Sorts, and Filters:

  • The Category model implements the HasAllowedFields, HasAllowedSorts, and HasAllowedFilters contracts, which define the fields, sorts, and filters that can be used in queries.
    public static function getAllowedFields(): array
    {
    return [];
    }

    public static function getAllowedSorts(): array
    {
    return [];
    }

    public static function getAllowedFilters(): array
    {
    return [];
    }

for more info check the package Rupadana\ApiService docs

Table Columns

  • parent_id
  • name
  • slug
  • description
  • position
  • is_visible
  • seo_title
  • seo_description
  • reference_id