Spec.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. */
  39. class Spec extends BaseModel
  40. {
  41. use SoftDeletes;
  42. const FORMAT_ATTR = "attr";
  43. protected $table = "spec";
  44. public function attrs()
  45. {
  46. return $this->hasMany(SpecAttr::class, "spec_id", "id");
  47. }
  48. public function format(...$options)
  49. {
  50. $return = [
  51. "id" => $this->id,
  52. "name" => $this->name,
  53. "is_custom" => (bool)$this->is_custom,
  54. "index_weight" => $this->index_weight,
  55. "category_weight" => $this->category_weight,
  56. "search_weight" => $this->search_weight,
  57. "created_at" => $this->created_at->format("Y-m-d"),
  58. ];
  59. if (in_array(self::FORMAT_ATTR, $options)) {
  60. $return['attr'] = $this->attrs->map(function (SpecAttr $m) {
  61. return [
  62. "id" => $m->id,
  63. "name" => $m->name,
  64. ];
  65. });
  66. }
  67. return $return;
  68. }
  69. }