12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Models\Goods;
- use App\Base\BaseModel;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Arr;
- class Spec extends BaseModel
- {
- use SoftDeletes;
- const FORMAT_ATTR = "attr";
- protected $table = "spec";
- public function attrs()
- {
- return $this->hasMany(SpecAttr::class, "spec_id", "id");
- }
- public function format(...$options)
- {
- $return = [
- "id" => $this->id,
- "name" => $this->name,
- "is_custom" => (bool)$this->is_custom,
- "index_weight" => $this->index_weight,
- "category_weight" => $this->category_weight,
- "search_weight" => $this->search_weight,
- "created_at" => $this->created_at->format("Y-m-d"),
- ];
- if (in_array(self::FORMAT_ATTR, $options)) {
- $return['attr'] = $this->attrs->map(function (SpecAttr $m) {
- return [
- "id" => $m->id,
- "name" => $m->name,
- ];
- });
- }
- return $return;
- }
- }
|