DistProduct.php 1.4 KB

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