Files
sam-api/app/Models/Products/Product.php

67 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Products;
use App\Models\Commons\Category;
2025-07-29 17:20:44 +09:00
use App\Models\Commons\File;
use App\Models\Commons\Tag;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $fillable = [
'tenant_id','code','name','category_id',
'product_type', // 라벨/분류용
'attributes','description','is_active',
'is_sellable','is_purchasable','is_producible',
'created_by','updated_by'
];
protected $casts = [
'attributes' => 'array',
'is_active' => 'boolean',
'is_sellable' => 'boolean',
'is_purchasable' => 'boolean',
'is_producible' => 'boolean',
];
// 분류
public function category() { return $this->belongsTo(Category::class, 'category_id'); }
2025-07-29 13:00:25 +09:00
// BOM (자기참조) — 라인 모델 경유
public function componentLines() { return $this->hasMany(ProductComponent::class, 'parent_product_id'); } // 라인들
public function parentLines() { return $this->hasMany(ProductComponent::class, 'child_product_id'); } // 나를 쓰는 상위 라인들
// 편의: 직접 children/parents 제품에 접근
public function children()
2025-07-29 13:00:25 +09:00
{
return $this->belongsToMany(
self::class, 'product_components', 'parent_product_id', 'child_product_id'
)->withPivot(['quantity','sort_order','is_default'])
->withTimestamps();
2025-07-29 13:00:25 +09:00
}
2025-07-29 17:20:44 +09:00
public function parents()
2025-07-29 17:20:44 +09:00
{
return $this->belongsToMany(
self::class, 'product_components', 'child_product_id', 'parent_product_id'
)->withPivot(['quantity','sort_order','is_default'])
->withTimestamps();
2025-07-29 17:20:44 +09:00
}
// 파일 / 태그 (폴리모픽)
public function files() { return $this->morphMany(File::class, 'fileable'); }
public function tags() { return $this->morphToMany(Tag::class, 'taggable'); }
// 스코프
public function scopeType($q, string $type) { return $q->where('product_type', $type); }
public function scopeSellable($q) { return $q->where('is_sellable', 1); }
public function scopePurchasable($q) { return $q->where('is_purchasable', 1); }
public function scopeProducible($q) { return $q->where('is_producible', 1); }
}