'json']; /** * 模型的 "booted" 方法 * @return void */ protected static function booted() { parent::booted(); static::saved(function ($product) { /** 同步信息到代理商产品 START */ AgentProduct::where(['type' => 0, 'product_id' => $product->id]) ->update([ 'title' => $product->title, 'know' => $product->know, 'content' => $product->content, 'pictures' => $product->pictures, 'extends' => $product->extends, 'longitude' => $product->longitude, 'latitude' => $product->latitude, 'address' => $product->address, ]); //UPDATE agent_products ap //INNER JOIN products p ON ap.product_id=p.id AND ap.product_id>0 //SET ap.latitude=p.latitude, ap.longitude=p.longitude, ap.address=p.address; /** 同步信息到代理商产品 END */ if ($product->isDirty('status') && $product->status == ProductStatus::ON_SALE) { ProductSaved::dispatch($product); } }); # 删除产品同步删除代理商产品 static::deleted(function ($product) { AgentProduct::query()->where('product_id', $product->id)->delete(); }); } // 获取所有产品图片 public function getPicturesAttribute($value): array { if (is_string($value)) { $value = $value ? json_decode($value, true) : []; } $this->append('picture'); return $value ?? []; } // 获取第一张产品图片 public function getPictureAttribute($value): string { return $this->pictures[0] ?? ''; } public function setPicturesAttribute($value) { if (is_array($value)) { $this->attributes['pictures'] = json_encode(array_filter($value)); } } public function supplier() { return $this->belongsTo(Supplier::class)->withTrashed(); } public function category() { return $this->belongsTo(Category::class); } public function spec() { return $this->hasMany(ProductSpec::class); } public function diyForm() { return $this->belongsTo(DiyForm::class); } }