hasOne(ProductCompatTypesModel::class, 'id', 'type_id'); } // 关联配件类型 public function parts() { return $this->hasOne(ProductCompatPartsModel::class, 'id', 'part_id'); } // 关联兼容型号 public function compatibleModels() { return $this->hasMany(ProductCompatModelsModel::class, 'compat_id'); } // 按所属语言查询 public function scopeLanguage(\think\db\Query $query, int $value) { $query->where('language_id', $value); } // 按类型查询 public function scopeTypeId(\think\db\Query $query, int $value) { $query->where('type_id', '=', $value); } // 按配件查询 public function scopePartId(\think\db\Query $query, int $value) { $query->where('part_id', '=', $value); } // 按型号查询 public function scopeModelName(\think\db\Query $query, string|array $value) { if (is_array($value)) { $query->where('model', 'in', $value); return; } $query->where('model', '=', $value); } // 按品牌名称搜索 public function searchBrandNameAttr(\think\db\Query $query, string|null $value, array $data) { if (is_null($value)) return; $query->where('brand_name', 'like', '%' . $value . '%'); } // 按型号搜索 public function searchModelAttr(\think\db\Query $query, string|null $value, array $data) { if (is_null($value)) return; $query->where('model', 'like', '%' . $value . '%'); } // 按兼容型号搜索 public function searchCompatibleModelAttr(\think\db\Query $query, string|null $value, array $data) { if (is_null($value)) return; $table = $this->getTable(); $relation_table = ProductCompatModelsModel::getTable(); $where = ProductCompatModelsModel::where($relation_table . '.compatible_model', 'like', '%' . $value . '%') ->where('compat_id', '=', $table . '.id') ->fetchSql(true) ->select(); $query->whereExists($where); } }