BaseProduct.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Dcat\Admin\Traits\ModelTree;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Spatie\EloquentSortable\Sortable;
  7. use Spatie\EloquentSortable\SortableTrait;
  8. class BaseProduct extends Model implements Sortable
  9. {
  10. use HasDateTimeFormatter;
  11. use SortableTrait;
  12. protected $table = 'base_product';
  13. // 可选:你可以在这里自定义排序配置
  14. public $sortable = [
  15. 'order_column_name' => 'order', // 排序字段
  16. 'sort_when_creating' => false, // 创建时自动排序
  17. ];
  18. protected $casts = [
  19. 'created_at' => 'datetime:Y-m-d H:i:s',
  20. 'updated_at' => 'datetime:Y-m-d H:i:s',
  21. 'parameters' => 'json', // 将 attributes 字段转换为数组
  22. ];
  23. public function baseProductCategory()
  24. {
  25. return $this->hasOne(BaseProductCategory::class,'id','category_id');
  26. }
  27. // 一对多关联
  28. public function images()
  29. {
  30. return $this->hasMany(BaseProductImage::class, 'product_id');
  31. }
  32. }