Spec.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Models\Goods;
  3. use App\Base\BaseModel;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Support\Arr;
  6. /**
  7. * App\Models\Goods\Spec
  8. *
  9. * @property int $id
  10. * @property string $name
  11. * @property array $extra
  12. * @property int $index_weight
  13. * @property int $category_weight
  14. * @property int $search_weight
  15. * @property int $is_custom 是否自定义
  16. * @property \Illuminate\Support\Carbon|null $created_at
  17. * @property \Illuminate\Support\Carbon|null $updated_at
  18. * @property \Illuminate\Support\Carbon|null $deleted_at
  19. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\SpecAttr[] $attrs
  20. * @property-read int|null $attrs_count
  21. * @method static \Illuminate\Database\Eloquent\Builder|Spec newModelQuery()
  22. * @method static \Illuminate\Database\Eloquent\Builder|Spec newQuery()
  23. * @method static \Illuminate\Database\Query\Builder|Spec onlyTrashed()
  24. * @method static \Illuminate\Database\Eloquent\Builder|Spec query()
  25. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereCategoryWeight($value)
  26. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereCreatedAt($value)
  27. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereDeletedAt($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereExtra($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereId($value)
  30. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereIndexWeight($value)
  31. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereIsCustom($value)
  32. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereName($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereSearchWeight($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereUpdatedAt($value)
  35. * @method static \Illuminate\Database\Query\Builder|Spec withTrashed()
  36. * @method static \Illuminate\Database\Query\Builder|Spec withoutTrashed()
  37. * @mixin \Eloquent
  38. * @property string $en_name
  39. * @method static \Illuminate\Database\Eloquent\Builder|Spec whereEnName($value)
  40. */
  41. class Spec extends BaseModel
  42. {
  43. use SoftDeletes;
  44. const FORMAT_ATTR = "attr";
  45. protected $table = "spec";
  46. public function attrs()
  47. {
  48. return $this->hasMany(SpecAttr::class, "spec_id", "id");
  49. }
  50. public function format(...$options)
  51. {
  52. $return = [
  53. "id" => $this->id,
  54. "name" => $this->name,
  55. "en_name" => $this->en_name,
  56. "is_custom" => (bool)$this->is_custom,
  57. "index_weight" => $this->index_weight,
  58. "category_weight" => $this->category_weight,
  59. "search_weight" => $this->search_weight,
  60. "created_at" => $this->created_at->format("Y-m-d"),
  61. ];
  62. if (in_array(self::FORMAT_ATTR, $options)) {
  63. $return['attr'] = $this->attrs->map(function (SpecAttr $m) {
  64. return [
  65. "id" => $m->id,
  66. "name" => $m->name,
  67. ];
  68. });
  69. }
  70. return $return;
  71. }
  72. }