Spec.php 3.3 KB

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