BaseProduct.php 1.4 KB

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