$this->getPrimaryId(), ]; $array = array_merge($array,$collage_array); $collageGroup = new CollageGroup($array); $collageable->collageGroup()->save($collageGroup); return $collageGroup; } public function checkCollageGroup(Collageable $collageable){ $now = date('Y-m-d H:i:s'); $result = CollageGroup::where('collageable_id',$collageable->getPrimaryId()) ->where('collageable_type',$this->getCurrentClass($collageable)) ->where('start_time','>',$now) ->where('end_time', '<',$now) ->exists(); return $result; } /** * @return MorphMany */ public function collageGroup(): MorphMany { return $this->morphMany(CollageGroup::class,'collageable'); } public function getPrimaryId(): string { return (string)$this->getAttribute($this->primaryKey); } public function getCurrentClass(Collageable $collageable){ return get_class($collageable); } /** * 是否开启拼团 */ public function hasGroup(Collageable $collageable){ $now = date('Y-m-d H:i:s'); $result = CollageGroup::where('collageable_id',$collageable->getPrimaryId()) ->where('collageable_type',$this->getCurrentClass($collageable)) ->where('start_time','<=',$now) ->where('end_time','>',$now) ->exists(); return $result; } /** * 拼团信息 */ public function groupInformation(Collageable $collageable){ $result = CollageGroup::where('collageable_id',$collageable->getPrimaryId()) ->where('collageable_type',$this->getCurrentClass($collageable)) ->first(); return $result; } }